Home
VB.NET
XElement Example
This page was last reviewed on Feb 16, 2023.
Dot Net Perls
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.
<?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.
Detail We call Elements() in the iteration statement of our ForEach loop. This returns an IEnumerable of XElements.
Detail We use Element() to get the loc element, which is nested with the url element.
Detail We invoke the Count() extension method from LINQ to count all the url elements in the XML file.
LINQ
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 Module
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.
IEnumerable
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.