When there was XmlDocument why we need XDocument, is there any better feature available with XDocument, answer is yes, XDocument has a number of benefits, easy to create, easy to update, easy to search, ability to use LINQ to XML, cleaner object model etc.
In this post we will see some examples and difference of code while using XmlDocument and XDocument; we will discuss some way to convert XmlDocument to XDocument. Let’s first see code difference to create a simple XmlDocument and XDocument and see the easiness of XDocument creation
<root name="rootname"> <child>Child Node Text</child> </root> // By using XmlDocument XmlDocument doc = new XmlDocument(); XmlElement root = doc.CreateElement("root"); root.SetAttribute("name", "rootname"); XmlElement child = doc.CreateElement("child"); child.InnerText = "Child Node Text"; root.AppendChild(child); doc.AppendChild(root); // By using XDocument XDocument doc = new XDocument(new XElement("root", new XAttribute("name", " rootname"), new XElement("child", "Child Node Text")));
In most of our existing applications we used XMLDocument, so how to convert and existing XmlDocument to XDocument, there are many ways, let's see some of them
//By using outerXml XDocument xdoc = XDocument.Parse(xmldoc.OuterXml); //By using CreateNavigator().ReadSubtree() XDocument xdoc = XDocument.Load(xmldoc.CreateNavigator().ReadSubtree()); //By using XmlNodeReader XDocument xdoc = XDocument.Load(new XmlNodeReader(xmldoc)); //Where xmldoc is the XmlDocument
This small peace of code shows the clear difference and easy to use of XDocument.
Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
|By Ali Adravi On 16 Mar, 13 Viewed: 1,070|
Reading and writing in xml can be done in different ways, some common classes people use are XmlTextReader, XmlTextWriter , XmlDocument but we will use XDocument. By using the other classes say XmlTextReader we need to use the while loop to read and check every node by using XmlNodeType, for a long... By Ali Adravi On 20 Dec 2014 Viewed: 1,758
Convert XML or JSON into a class by using visual studio is as easy as just copy and two clicks, never matter how big or how complicated is our XML or JSON. In this article we will create some dummy XML and json and will try to convert them into class without writing a single property manually. It... By Ali Adravi On 20 Dec 2014 Viewed: 732
HTML syntax, this is the format suggested for most authors. It is compatible with most legacy Web browsers. If a document is transmitted with the `text/html MIME` type, then it will be processed as an HTML document by Web browsers. This specification defines version 5 of the HTML syntax, known as... By Alicia Gonzalez On 25 Dec 2012 Viewed: 421
XML (Extensible Markup Language) is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more. It was derived from an older standard format called SGML. **XML Used For** XML is one of the most widely-used... By Alicia Gonzalez On 25 Dec 2012 Viewed: 296
In some situations we need to create website menu from database, say our application have ability to create pages so that must also need to have a menu item to go to that page. So how to create menu with all those nice CSS and other settings, I think XSLT is best to use in these... By Hamden On 16 Mar 2013 Viewed: 791