
You want to load and parse an XML document in your program targeting the .NET Framework. Use the new XElement in System.Xml.Linq, which allows you to remove lots of old code and eliminate the possibility of bugs and typos. Here we see an example of XElement from LINQ, using it to load an XML document and then query it using expressions, using the C# programming language.
Here we look at how you can use the static Load method on an XML document with XElement. XElement and XDocument make using XML easy. To load an XML document into memory you only need one line of code. Then you can use methods on the XElement object to access the elements.
Class that uses XElement [C#]
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Xml.Linq;
public sealed class SiteStructure
{
static readonly SiteStructure _instance = new SiteStructure();
public static SiteStructure Instance
{
get { return _instance; }
}
XElement _x;
private SiteStructure()
{
string mapLoc = HttpContext.Current.Request.MapPath("~/App_Data/Map1.xml");
_x = XElement.Load(mapLoc);
}
}
Overview. It uses MapPath. This allows you to go from a virtual path to a real server path, meaning you can open an XML file by its name easily. XElement.Load reads in the data. This is a static method on the XElement class that allows you to easily load the XML into memory. The whole file is now parsed and in memory.
MapPath Resolves Virtual, Physical PathsNotes on XML files. We are using the App_Data folder because it is a special ASP.NET folder for database files or XML files. You could have an XML file in your App_Data folder called Map1.xml. Look at the App_Code folder and then add a new C# class. You can make the class a singleton that stores an XElement.
Singleton ClassHere we see that you can query the XML you loaded with XElement as well. We are using the XML like a database, but it is entirely in-memory and the queries are native. Look at how the query expressions are used in this example.
Method that uses XElement [C#]
public void Example()
{
// Get pages matching the title specified.
string t = "Some title";
var v = from page in _x.Elements("SitePage")
where t == page.Element("Title").Value
select page;
// Get category of first page matched.
string c = v.First().Element("Category").Value;
// Count number of elements with that category element value.
int count = (from p in _x.Elements("SitePage")
where p.Element("Category").Value == c &&
p.Element("Visibility").Value == "Regular"
select p).Count();
}
Here you can see the XML used for this article. You can match up the strings used in the above code samples with the tag and attribute names. This part of the article is completely application-specific, and your XML will be very different. However, you can use some of the code samples in this article to help you deal with your XML.
File used [XML]
<?xml version="1.0"?>
<ArrayOfSitePage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SitePage>
<Visibility>Supplementary</Visibility>
<Title>C# .NET Examples and Resources</Title>
<Url></Url>
<Category>None</Category>
</SitePage>
<SitePage>
<Visibility>Supplementary</Visibility>
<Title>Usability Guidelines for Web Writing</Title>
<Url>Content/Usability-Writing.aspx</Url>
<Category>None</Category>
</SitePage>
<SitePage>
<Visibility>Regular</Visibility>
<Title>Alphanumeric Sorting in C#</Title>
<Url>Content/Alphanumeric-Sorting.aspx</Url>
<Category>Algorithms</Category>
</SitePage>
<SitePage>
<Visibility>Regular</Visibility>
<Title>Word Count Algorithm in C#</Title>
<Url>Content/Word-Count-Algorithm.aspx</Url>
<Category>Algorithms</Category>
</SitePage>
</ArrayOfSitePage>
We saw how you can use XElement and its Load method to quickly and effortlessly load XML. Load your XML file in a single statement and then run queries on it to generate output. Improvements could store more detailed data or be modified at runtime. XElement performs well but keep in mind issues with performance when using LINQ.
LINQ Examples XML Tutorials