TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

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.dotnetCodex.com</loc></url> <url><loc>http://www.dotnetCodex.com/24-hour-time</loc></url> <url><loc>http://www.dotnetCodex.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.

LINQ
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.dotnetCodex.com http://www.dotnetCodex.com/24-hour-time http://www.dotnetCodex.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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf