0

I have a requirement where in i have been asked to create a xml file which contains some hard coded tags and a part of which will contain data from sql.

here is code..

new XElement("batchContactList",
                 new XElement("contact", new XAttribute("contactID", "0003121"), new XAttribute("action", "AddOrModify"),
                      new XComment("The only required fields for a contact are contactID, action, FirstName,LastName and one valid contactPoint Options for action are: Add, Modify, AddOrModify, Remove, Ignore"),
                      //Data from SQL using linq to SQl
                      from employee in db.Employees select new XElement("contactField", employee.firstName, new XAttribute("name","FirstName")),
                      from employee in db.Employees select new XElement("contactField", employee.lastName, new XAttribute("name", "LastName"))
                      //foreach (var Emp in db.Employees)
                      // {
                          // new XElement("contactField", Emp.firstName, new XAttribute("name","FirstName"));
                      // }
                     )
                   )

the output that currently am getting is :

<batchContactList xmlns="">
  <contact contactID="0003121" action="AddOrModify">
  <!-- The only required fields for a contact are contactID, action, 
       FirstName,LastName and one valid          
       contactPoint Options for action are: Add, Modify, AddOrModify, Remove, Ignore
    --> 
       <contactField name="FirstName">TestFirstName</contactField> 
       <contactField name="FirstName">TestFname</contactField> 
       <contactField name="LastName">TestLastName</contactField> 
       <contactField name="LastName">TestLNmae</contactField> 
   </contact>
</batchContactList>

Desired output XML:

<batchContactList>
    <contact contactID="0003121" action="AddOrModify">
           <!--The only required fields for a contact are contactID, action, FirstName,
            LastName and one valid contactPoint
            Options for action are:
            Add, Modify, AddOrModify, Remove, Ignore  -->
         <contactField name="Pin">3121</contactField>
         <contactField name="FirstName">Patrick</contactField>
          <contactField name="LastName">Bateman</contactField>
          <contactField name="MiddleName">J</contactField>
          <contactField name="CustomField" customName="Department">IT Support</contactField>
          <contactField name="CustomField" customName="Title">Sr. Database Admin</contactField>
          <contactField name="Address1">224 W. 30th Street</contactField>
          <contactField name="Address2">Suite 500</contactField>
          <contactField name="City">New York</contactField>
          <contactField name="State">NY</contactField>
          <contactField name="PostalCode">10001</contactField>
          <contactField name="Country">United States</contactField>
          <contactField name="TimeZone">US/Eastern</contactField>

          <!-- contactLogin is optional; if used, all elements are required -->
          <contactLogin username="user3121" password="pswd3121">
              <quickSendCode>quick3121</quickSendCode>
              <accessGroupsList>
                <groupName>IT</groupName>
              </accessGroupsList>
              <status>Enabled</status>
          </contactLogin>

         <!-- There is no limit to the number of group memberships-->
         <groupList>
             <group desc="Information Technology" name="IT"/>
             <group desc="Engineering" name="ENG"/>
        </groupList>

         <!-- Up to 5 Voice plus 5 Email or TextMessage contact points-->
        <contactPointList>
            <contactPoint type="Voice">
                <contactPointField name="Label">Cell</contactPointField>
                <contactPointField name="CountryCode">1</contactPointField>
                <contactPointField name="Number">7185550000</contactPointField>
                <contactPointField name="Extension">1212</contactPointField>
            </contactPoint>
            <contactPoint type="Email">
                <contactPointField name="Label">Email</contactPointField>
                <contactPointField name="Address">[email protected]</contactPointField>
            </contactPoint>
        </contactPointList>
    </contact>
    <!-- These examples contains only the required fields -->
    <contact contactID="0000413" action="Add">
        <contactField name="FirstName">Sam</contactField>
        <contactField name="LastName">Sample</contactField>
        <contactPointList>
            <contactPoint type="Email">
                <contactPointField name="Label">Email</contactPointField>
                <contactPointField name="Address">[email protected]</contactPointField>
            </contactPoint>
        </contactPointList>
    </contact>
    <contact contactID="0000666" action="Modify">
        <contactField name="FirstName">Joe</contactField>
        <contactField name="LastName">Sample</contactField>
        <contactPointList>
        <contactPoint type="Email">
            <contactPointField name="Label">Email</contactPointField>
            <contactPointField name="Address">[email protected]</contactPointField>
        </contactPoint>
        </contactPointList>
    </contact>
</batchContactList>

the code that wrote is incomplete for all the fields as you see i was not getting the output that i needed.

Also suggest me if there is a better wat of doing,,

can post the complete code if you want...

2
  • You don;t mention which version or kind of SQL you are using... but technet.microsoft.com/en-us/library/ms178107.aspx explains the sometimes very handy ability of SQL server 2012 to return XML for you :) Commented Jan 9, 2014 at 8:37
  • Am using sql server 2008 r2. Yes sql server do support converting result set to xml,, but there are other plenty of hard coded tags needs to be written .. so i thought of linq to xml will be the best choice here.. Commented Jan 9, 2014 at 9:24

1 Answer 1

1

You should be able to achieve the desired output using a combination of LINQ to XML and LINQ to SQL. The following MSDN Blog provides a good overview:

http://blogs.msdn.com/b/spike/archive/2010/01/11/how-to-use-linq-to-xml-together-with-linq-to-sql.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.