使用linq c#从sql数据库创建xml文件

时间:2014-01-09 08:31:07

标签: sql xml

我有一个要求,在我被要求创建一个xml文件,其中包含一些硬编码标签,其中一部分将包含来自sql的数据。

这是代码..

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"));
                      // }
                     )
                   )

目前正在获得的输出是:

<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>

所需的输出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">example@sendwordnow.com</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">sam@sendwordnow.com</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">joe@sendwordnow.com</contactPointField>
        </contactPoint>
        </contactPointList>
    </contact>
</batchContactList>

所写的代码对于所有字段都是不完整的,因为你看到我没有得到我需要的输出。

还建议我,如果有更好的操作,

如果你想要

,可以发布完整的代码。

1 个答案:

答案 0 :(得分:1)

您应该能够使用LINQ to XML和LINQ to SQL的组合来实现所需的输出。以下MSDN博客提供了一个很好的概述:

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