VB.NET XElement Example

Use the XElement class and the XElement.Load function. Loop over nested XML elements.

XElement. Parsing XML files is complicated. With the XElement type in newer versions of VB.NET, we can load XML and loop over its elements quickly.

Nested elements are supported. To get nested elements, we call the Elements() Function on an XName or XElement instance. This is slower than custom parsers, but reliable.

Example XML file. This tutorial uses a sitemap XML file. This contains a urlset element with url and loc elements nested within it. The file is a valid sitemap.
Tip: Please place this file in a convenient location, like a "programs" folder on your C disk volume.
File: sitemap.xml <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> <url><loc>http://www.dotnetperls.com</loc></url> <url><loc>http://www.dotnetperls.com/24-hour-time</loc></url> <url><loc>http://www.dotnetperls.com/2d-array</loc></url> </urlset>

Load, Elements example. Here we use the XElement.Load function on the sitemap's file path. We use XName.Get to get XName instances that match the url and loc tags.
Elements: We call Elements() in the iteration statement of our ForEach loop. This returns an IEnumerable of XElements.
Element: We use Element() to get the loc element, which is nested with the url element.
Count: We invoke the Count() extension method from LINQ to count all the url elements in the XML file.
VB.NET program that uses XElement, Load Module Module1 Sub Main() ' Load sitemap XML file from local file. Dim sitemap As XElement = XElement.Load("C:\\programs\\sitemap.xml") Dim url As XName = XName.Get("url", "http://www.sitemaps.org/schemas/sitemap/0.9") Dim loc As XName = XName.Get("loc", "http://www.sitemaps.org/schemas/sitemap/0.9") ' Use Elements to loop over XElements. For Each urlElement As XElement In sitemap.Elements(url) ' Get subelement from URL. ' ... This is a LOC. Dim locElement As XElement = urlElement.Element(loc) Console.WriteLine(locElement.Value) Next ' Count elements. Console.WriteLine("Count: {0}", sitemap.Elements(url).Count()) End Sub End Module Output http://www.dotnetperls.com http://www.dotnetperls.com/24-hour-time http://www.dotnetperls.com/2d-array Count: 3

IEnumerable. The XElement classes support the IEnumerable interface. This makes it possible to use LINQ extensions to query sub-elements.
Tip: In the above example, the Count() extension acts upon IEnumerable. And For Each requires an IEnumerable interface.
IEnumerableFor Each, For

Performance considerations. The XElement classes are not fast. In programming, using a low-level parser, that works char by char, is usually the fastest solution.
However: Code that uses XElement is clear. It is easy to validate and it will work correctly.
So: In programs that do not require extreme performance, XElement is a good choice. It is a reliable option.

A short summary. XElement is a modern way of handling XML data in the .NET Framework. It is not optimally fast. We pay this price for its clarity and ease of use.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.