LINQ To XML
LINQ To XML
LINQ to XML is a built-in LINQ data provider that provides a clean programming model to read, construct and write XML data. We can use LINQ to XML to perform LINQ queries over XML. It provides much richer querying support than the low-level XmlReader/XmlWriter API in .NET. It uses less memory and more efficient than the XmlDocument, which is built on Document Object Model(DOM). LINQ to XML allows to write Query Expressions over XML and can be combined with any of the other LINQ providers to create or use XML data as a source or destination format. LINQ to XML uses very simple model for constructing XML documents. Whether it be XML sources from a stream or file, or XML created dynamically in the code.
XDeclaration, XComment, XProcessingInstruction classes are used to define xml declaration, xml comments and xml processing instructions respectively. SalesOrder object is an XElement which holds the Salesorder xml. XDocument class is used to represent the xml as a XMLDocument.
XElement salesOrder = XElement.Load(@"c:\po.xml"); // to load from a file XElement salesOrder = XElement.Load(new XmlTextReader(@"c:\po.xml")); // loading from a XML Text Reader XElement.Load method is used to load the xml into XElement from any file or TextReader. We can also use Parse method of the XElement to load the xml from a string . XElement salesOrder = XElement.Parse( @"<SALESORDER> <SOHEADER> <ORDERID>140310915</ORDERID> <SALESREP>GLATMAN, AARON</SALESREP> <CUSTOMERNAME>Quiksilver America</CUSTOMERNAME> <CUSTOMERPONUMBER></CUSTOMERPONUMBER> <SHIPTOADDRESS>QUIKSILVER INC</SHIPTOADDRESS> <INVOICEADDRESS>QUIKSILVER INC</INVOICEADDRESS> </SOHEADER> <SOLINES> <ITEMNAME>1006</ITEMNAME> <QUANTITY>3</QUANTITY> <UNITPRICE>.01999</UNITPRICE> </SOLINES> <SOLINES> <ITEMNAME>1006</ITEMNAME> <QUANTITY>3</QUANTITY> <UNITPRICE>.01999</UNITPRICE> </SOLINES> </SALESORDER>");
Traversing XML
We can navigate the LINQ to XML Object Model to retrieve the XML elements that we want to work on. LINQ to XML provides methods for getting the children of an XElement. Nodes() method is used to get all of the children of an XElement (or XDocument).This returns IEnumerable<object>. foreach (c in salesOrder .Nodes()) { Console.WriteLine(c); } We can navigate to a particular XElement in a XElement or XDocument by using Element(string name) method as shown below. XElement header = salesOrder.Element("SOHEADER"); XElement orderid = header.Element("ORDERID"); Header obtains a reference to SOHEADER node and orderid gets the ORDERID of the order. We can even navigate all the elements of a XElement or XDocument by using the Elements() method. Elements method returns IEnumerable<XElement>. foreach (XElement x in header.Elements()) { Label lb=new Label(); lb.Text = x.Name.ToString(); TextBox tb = new TextBox(); tb.Text = x.Value; form.Controls.Add(new LiteralControl("<tr><td>")); form.Controls.Add(lb); form.Controls.Add(new LiteralControl("</td><td>")); form.Controls.Add(tb); form.Controls.Add(new LiteralControl("</td></tr>")); form.Controls.Add(new LiteralControl("<p><p>"));
} form.Controls.Add(new LiteralControl("</table>")); form.Controls.Add(new LiteralControl("<table>")); foreach (XElement lines in salesOrder.Elements("SOLINES")) { form.Controls.Add(new LiteralControl("<tr>")); foreach (XElement x in lines.Elements()) { Label lb=new Label(); lb.Text = x.Name.ToString(); TextBox tb = new TextBox(); tb.Text = x.Value; form.Controls.Add(new LiteralControl("<td>")); form.Controls.Add(lb); form.Controls.Add(new LiteralControl("</td><td>")); form.Controls.Add(tb); form.Controls.Add(new LiteralControl("</td>")); } form.Controls.Add(new LiteralControl("</tr>")); form.Controls.Add(new LiteralControl("</table>")); }
salesOrder.Element("SOLINES").Elements()Remove(); The above statement removes all the elements from the first SOLINES XElement.