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.
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.
<?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>
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()
in the iteration statement of our ForEach
loop. This returns an IEnumerable
of XElements
.Element()
to get the loc element, which is nested with the url element.Count()
extension method from LINQ to count all the url elements in the XML file.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 sub-element 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 Modulehttp://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.
Count()
extension acts upon IEnumerable
. And For Each
requires an IEnumerable
interface
.The XElement
classes are not fast. In programming, using a low-level parser, that works char
by char
, is usually the fastest solution.
XElement
is clear. It is easy to validate and it will work correctly.XElement
is a good choice. It is a reliable option.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.