C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
XName: We create 2 XName objects. These store both the namespace and the local element name.
Note: For XML files in a custom namespace, use XName objects instead of strings to look up elements in XElement objects.
Elements: We call the Elements() method with the XName argument. This returns all url elements in the sitemap.
Info: The program calls the Count() extension method to count all the Elements(). This is needed on an IEnumerable.
CountIEnumerableC# program that uses XElement.Load on sitemap
using System;
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main()
{
// ... Download sitemap.
XElement sitemap = XElement.Load("http://www.dotnetCodex.com/sitemap.xml");
// ... XNames.
XName url = XName.Get("url", "http://www.sitemaps.org/schemas/sitemap/0.9");
XName loc = XName.Get("loc", "http://www.sitemaps.org/schemas/sitemap/0.9");
// ... Loop over url elements.
// ... Then access each loc element.
foreach (var urlElement in sitemap.Elements(url))
{
var locElement = urlElement.Element(loc);
Console.WriteLine(locElement.Value);
}
// ... Display count.
Console.WriteLine("Count: {0}", sitemap.Elements(url).Count());
}
}
End of sample XML file: sitemap.xml
<url>
<loc>http://www.dotnetCodex.com/xor</loc>
<image:image><image:loc>http://www.dotnetCodex.com/xor.png</image:loc></image:image>
</url>
<url>
<loc>http://www.dotnetCodex.com/yield</loc>
<image:image><image:loc>http://www.dotnetCodex.com/yield.png</image:loc></image:image>
</url>
<url>
<loc>http://www.dotnetCodex.com/zip</loc>
</url>
</urlset>
End of output
http://www.dotnetCodex.com/xor
http://www.dotnetCodex.com/yield
http://www.dotnetCodex.com/zip
Count: 1247
Then: You can use methods on the XElement, such as Element() and Elements(), to access the XML elements.
MapPath: MapPath translates from a virtual path to a real server path. This means you can easily open an XML file by its name.
Load: XElement.Load reads in the data. This is a static method that allows you to load the XML into memory.
MapPathStaticSo: At this point, the whole XML file is now parsed and resident in memory. No further file system or network accesses are needed.
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);
}
}
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();
}
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>