Introduction
This article demonstrates how to parse XML documents using "XMLDocument" as well as "XDocument".
Background
"XML" and the parsing of XML documents are very old topics and it is not very common for us to directly work on XML documents. But once a while, you may find that you are given some raw XML documents and you need to parse them. This article is to demonstrate how to use the two commonly used .Net utility classes "XMLDocument" and "XDocument" to parse XML documents
The attached Visual Studio 2010 solution has two projects:
- The "XMLWebRepository" project is an ASP.Net web project. The "Student.xml" file in this project is the XML file used the demonstration.
- The "XMLParsingExample" project is a simple WPF "MVVM" application. We will be using this application to demonstrate how to parse XML documents using "XMLDocument" as well as "XDocument".
Let us first take a look at the "XMLWebRepository" project and the XML document "Student.xml".
The XML document to parse
The XML document used in this demo is in the "Student.xml" file in the "XMLWebRepository" project:
<?xml version="1.0" encoding="utf-8" ?>
<StudentsInformation>
<GeneralInformation>
<School>University of XML</School>
<Department>Department of XML parsing</Department>
</GeneralInformation>
<Studentlist>
<Student id="1" score="100" enrollment="4/30/1789"
comment="1st and greatest president">George Washington</Student>
<Student id="2" score="100" enrollment="3/4/1861"
comment="Civil was. Great president!">Abraham Lincoln</Student>
<Student id="3" score="99" enrollment="1/20/1993"
comment="Monica Samille Lewinsky, Cool!">Bill J. Clinton</Student>
<Student id="4" score="98" enrollment="1/20/2001"
comment="House price went beyond American's affordability">George W. Bush</Student>
<Student id="5" score="99" enrollment="1/20/2009" comment=
"Ridiculously low interest rate, high house price. $ worthless">Barack H. Obama</Student>
Read more: Codeproject