Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Parsing/Reading XML from URL in VB.NET

0.00/5 (No votes)
27 Dec 2017 1  
This tip contains easy steps to extract XML data from URLs in VB.NET

Introduction

This tip shows an easy way to read or parse an XML Document from a URL, by using the MXMLParser class that I developed. The class uses the various objects and methods provided by the built-in XMLTextReader class. Information about XMLTextReader is available on MSDN.

Using the Code

Firstly, I will show you how MXMParser collects the data from the XML Document.

The class first needs initialization that can be done by creating a new object of the class.

Dim xm = New MXMLParser

After initialization, the MXMLParser.read() method is used to read the XML Document from the URL that is passed as parameter of type string.

xm.read(URL As String)

The read method creates a new XMLTextReader object that contains the method to read the document. The newly created object then reads the document node by node through its XMLTextReader.Read() method and based on the XMLTextReader.XMLNodeType, the data is collected in the form of xmlnode object. These XMLNodeTypes are currently included- Element, AttributeText and EndElement. If any other XMLNodeType is required you can request in comments. Click here for details of XMLNodeTypes.

The xmlnode object contains methods to store the tagname and attributes, and also the methods to retrieve this stored data {By getName() and getAttribute() methods}. The attributes are stored in the public variable xmlnode.attributes as an arraylist of attribute object.

The attribute object contains the name and value of the attribute that can be retrieved by attribute.getName() and attribute.getValue() methods.

In the process of data collection, the data is continuously being added to a string that is MXMLParser.xmlString. This string is the raw XML Document that can be used for manual parsing and for other purposes. This string does not contain the Node types except those mentioned above.

Below are some examples to give you the basic idea of how to use this class.

Example 1 - Getting the XML Document as String

Dim xm = New MXMLParser
xm.read(URL As String)
Dim Xmldoc As String
Xmldoc=xm.xmlString

Example 2 - Getting Data from Tags of Specific Tag Name

Suppose you need to get all the content form the title tags of the XML, then you can do something like this:

Dim titleArray As New ArrayList
For Each xmnode As MXMLParser.xmlnode In xm.read(URL here)
    If (xmnode.getName = "title") Then
       titleArray.Add(xmnode.getText)
    End If
Next

Example 3 - Getting Data from Tags having Specific Attribute Values

Suppose you need to get all the content from the tags that contain an attribute of name "attrib" having the value "hello", then you can do something like this:

Dim helloArray As New ArrayList
For Each xmnode As MXMLParser.xmlnode In xm.read(URL here)
  If (xmnode.attributes.Contains("attrib")) _
  Then 'check whether xmlnode contains "attrib" attribute.
        If (xmnode.getAttribute("attrib").getValue() = "hello") _
        Then 'check if value is "hello".
           helloArray.Add(xmnode.getText())
        End If
    End If
Next

The helloArray will contain the inner text of xmlnode objects which have attrib="hello"(<tagname attrib="hello">inner text</tagname>).

Or, if you only want those tags that contain the "attrib" attribute irrespective of their values, then:

For Each xmnode As MXMLParser.xmlnode In xm.read(URL here)
    if(xmnode.attributes.Contains("attrib") Then
       'code here
    End If
Next

Conclusion

This class simplifies reading and parsing of XML. If you can't understand any part of this tip, you can always comment and I will try my best to explain that part to you or edit that part to make it more understandable. You can also request additional features. If you have any suggestions about improving this class, then I will be happy to receive your opinions and update the class accordingly. If this tip helped you, be sure to leave a "Thank you" comment, it will make this programmer a little more happy ;).

History

This is the first version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here