Xml stands for eXtensible Markup Language that is designed to store and transfer data. An example xml file is below.
- <?xml version="1.0" encoding="utf-8" standalone="no"?>
- <!--demo on how to create xml file-->
- <root>
- <Country id="1" name="Australia" capital="Canberra">
- <state>New South Wales</state>
- <state>Victoria</state>
- </Country>
- <Country id="2" name="India" capital="New Delhi">
- <state>Maharashtra</state>
- <state>Rajasthan</state>
- </Country>
- <Country id="2" name="Bangladesh" capital="Dhaka"><![CDATA[Bangladesh doesn't have any states.]]></Country>
- </root>
Xml file starts the xml declaration as in the first line. It supports comments similar to html comments as shown in line 2. It can have elements, can have attributes and can contain CData that is data that can be considered as plain text. Line 4 shows an example of element and attributes – Country is an element and id, name and capital are attributes. The value 1, Australia, etc are the values of the attributes.
In .NET, Linq to Xml makes it easy to create and parse xml documents. It’s easy to create xml, add declarations, add elements, attributes and also add data appropriately. The necessary classes are within the System.Xml.Linq namespace. Linq to Xml has XDocument class with xml documents can be created and loaded. XComment class can be used to add comments. XElement can be used to create elements and XAttribute classes can be used to add attributes to elements. XCData class can be used to add data in cdata format. An example on how to create xml using Linq to Xml is below.
- private void CreateXml()
- {
- XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "no"));
- XComment comment = new XComment("demo on how to create xml file");
- doc.Add(comment);
- XElement el = new XElement("root");
- XElement p1 = new XElement("Country", new object[] { new XAttribute("id", "1"), new XAttribute("name", "Australia"), new XAttribute("capital", "Canberra") });
- XElement p1city1 = new XElement("state", "New South Wales");
- XElement p1city2 = new XElement("state", "Victoria");
- p1.Add(p1city1);
- p1.Add(p1city2);
- XElement p2 = new XElement("Country", new object[] { new XAttribute("id", "2"), new XAttribute("name", "India"), new XAttribute("capital", "New Delhi") });
- XElement p2city1 = new XElement("state", "Maharashtra");
- XElement p2city2 = new XElement("state", "Rajasthan");
- p2.Add(p2city1);
- p2.Add(p2city2);
- XElement p3 = new XElement("Country", new object[] { new XAttribute("id", "3"), new XAttribute("name", "Bangladesh"), new XAttribute("capital", "Dhaka") });
- p3.Add(new XCData("Bangladesh doesn't have any states."));
- el.Add(p1);
- el.Add(p2);
- el.Add(p3);
- doc.Add(el);
- doc.Save(Server.MapPath("country.xml"));
- }
As can be seen, once the XElement, XComment, XAttribute objects are created, they got to be added to parent object. Once the necessary data has been added, the xml file can be saved using XDocument’s Save method as in line 34 above.
0 comments:
Post a Comment