In .NET Framework 4.0 the System.Xml APIs are obsolete and are maintained for compatibility reasons only. Web developers can create XML file by using LINQ to XML. The next picture presents classes in LINQ to XML.
These classes are included in System.Xml.Linq namespace and the following ones are the most important, because they cover 90% of cases:
– XAttribute – Represents an attribute
– XComment – Represents a comment
– XDocument – Represents the XML
– XElement – Represents a node in the XML
– XDeclaration – Represents the XML declaration
1. Web developer has to create an XDocument instance and, in its constructor has to pass XElement instance that create the root node.
2. The XElement constructor takes a list of XElement instances that represent the children. Based on this design, Web developer can create the whole XML in a single statement, by nesting XElement instances. If his/her code is well structured it easy to understand.
XDocument myDoc = new XDocument(
new XDeclaration(“1.0”, “utf-8”, “true”),
new XComment(“my comment”),
new XAttribute(“ISBN-13”, “978-0545139700”),
new XElement(“Title”, ” Harry Potter and the Deathly Hallows “)
The result is the following:
<?xml version=”1.0” encoding=”utf-8”?>
<Title> Harry Potter and the Deathly Hallows</Title>
3. After he/she has the XDocument instance with the XML, Web developer can use the Save method to save the XML string in a file places wherever he/she wants