Monday, August 6, 2012

LINQ to XML -


LINQ to XML - How to create and save an XML document
CreateXMLDocument.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3. <%@ Import Namespace="System.Xml.Linq" %>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6. <script runat="server">  
  7.     protected void Button1_Click(object sender, System.EventArgs e)  
  8.     {  
  9.         XDocument xDoc = new XDocument  
  10.         (  
  11.             new XDeclaration("1.0","utf-8","yes"),  
  12.             new XComment("This is an Xml Document which programmatically created by LINQ to XML"),  
  13.             new XElement("employees",  
  14.                 new XElement("employee",  
  15.                     new XAttribute("id","101"),  
  16.                     new XElement("name","jenny jones"),  
  17.                     new XElement("city", "rome")  
  18.                              )  
  19.                          )  
  20.         );  
  21.   
  22.         String xmlFile = Server.MapPath("~/App_Data/Employee.xml");  
  23.         xDoc.Save(xmlFile);  
  24.   
  25.         XElement xFile = XElement.Load(xmlFile);  
  26.         TextBox1.Text = xFile.ToString();  
  27.     }  
  28. </script>  
  29.   
  30. <html xmlns="http://www.w3.org/1999/xhtml">  
  31. <head id="Head1" runat="server">  
  32.     <title>LINQ to XML - How to create and save an XML document</title>  
  33. </head>  
  34. <body>  
  35.     <form id="form1" runat="server">  
  36.     <div>  
  37.         <h2 style="color:DarkBlue; font-style:italic;">  
  38.             LINQ to XML - How to create and save an XML document  
  39.         </h2>  
  40.         <hr width="600" align="left" color="CornFlowerBlue" />  
  41.         <asp:TextBox  
  42.              ID="TextBox1"   
  43.              runat="server"   
  44.              TextMode="MultiLine"  
  45.              Columns="75"  
  46.              Rows="15"  
  47.              Enabled="false"  
  48.              >  
  49.         </asp:TextBox>  
  50.         <br />  
  51.         <asp:Button   
  52.             ID="Button1"  
  53.             runat="server"  
  54.             OnClick="Button1_Click"  
  55.             Text="Create and Save an XML Document"  
  56.             Height="45"  
  57.             Font-Bold="true"  
  58.             ForeColor="DodgerBlue"  
  59.             />  
  60.     </div>  
  61.     </form>  
  62. </body>  
  63. </html>  


 

 

 






LINQ to XML - How to generate XML from a current DataSource

Code:
XElement xml = new XElement("contacts",
                    from c in db.Contacts
                    orderby c.ContactId
                    select new XElement("contact",
                              new XAttribute("contactId", c.ContactId),
                              new XElement("firstName", c.FirstName),
                              new XElement("lastName", c.LastName))
                    );


Output:
<contacts>
  <contact contactId="2">
    <firstName>Barney</firstName>
    <lastName>Gottshall</lastName>
  </contact>
  <contact contactId="3">
    <firstName>Armando</firstName>
    <lastName>Valdes</lastName>
  </contact>
  <contact contactId="4">
    <firstName>Adam</firstName>
    <lastName>Gauwain</lastName>
  </contact>
  ...
</contacts>

here db is the datatable




Reading xml using linq


Once these files are loaded into the LINQ to XML API, you can write queries over that tree. The query syntax is easier than XPath or XQuery for developers who do not use XPath or XQuery on a daily basis.

Code:
// Loading from a file, you can also load from a stream
XDocument loaded = XDocument.Load(@"C:\contacts.xml");


// Query the data and write out a subset of contacts
var q = from c in loaded.Descendants("contact")
        where (int)c.Attribute("contactId") < 4
        select (string)c.Element("firstName") + “ “ +
      (string)c.Element("lastName");


foreach (string name in q)
    Console.WriteLine("Customer name = {0}", name);


Output:
Customer name = Barney Gottshall
Customer name = Armando Valdes

No comments:

Post a Comment