Click here to Skip to main content
15,886,095 members
Articles / Desktop Programming / WPF
Tip/Trick

Parsing/Reading XML from URL in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.87/5 (15 votes)
27 Dec 2017CPOL3 min read 70.2K   3.3K   28   20
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.

VB.NET
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.

VB.NET
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

VB.NET
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:

VB.NET
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:

VB.NET
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:

VB.NET
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
India India
I am an inspired developer. I enjoy developing applications for windows and android. I wish every other developer best of luck for their future work.
https://devgroup2017.wordpress.com/

Comments and Discussions

 
QuestionThe request was aborted: Could not create SSL/TLS secure channel. Pin
Member 881056724-Oct-20 2:59
Member 881056724-Oct-20 2:59 
QuestionXmlTextReader issue Pin
Rydo77730-Dec-17 12:33
Rydo77730-Dec-17 12:33 
QuestionxmlString missing Pin
Member 1358576419-Dec-17 21:34
Member 1358576419-Dec-17 21:34 
AnswerRe: xmlString missing Pin
Bhanu Pratap Singh Rathore27-Dec-17 6:35
Bhanu Pratap Singh Rathore27-Dec-17 6:35 
Question"Declaration Expected" error Pin
Member 135088237-Nov-17 13:51
Member 135088237-Nov-17 13:51 
AnswerRe: "Declaration Expected" error Pin
Bhanu Pratap Singh Rathore10-Nov-17 5:30
Bhanu Pratap Singh Rathore10-Nov-17 5:30 
QuestionCannot get your Example #1 to work Pin
theskiguy19-Jan-17 5:11
theskiguy19-Jan-17 5:11 
AnswerRe: Changes done. Pin
Bhanu Pratap Singh Rathore20-Jan-17 5:11
Bhanu Pratap Singh Rathore20-Jan-17 5:11 
GeneralGood component Pin
Sheo Narayan24-Dec-15 14:07
Sheo Narayan24-Dec-15 14:07 
QuestionGetting data from tags having specific attribute values Pin
Member 102537016-Dec-15 11:46
professionalMember 102537016-Dec-15 11:46 
AnswerRe: Getting data from tags having specific attribute values Pin
Bhanu Pratap Singh Rathore21-Dec-15 19:52
Bhanu Pratap Singh Rathore21-Dec-15 19:52 
GeneralRe: Getting data from tags having specific attribute values Pin
Member 1025370123-Dec-15 13:09
professionalMember 1025370123-Dec-15 13:09 
GeneralRe: Getting data from tags having specific attribute values Pin
Bhanu Pratap Singh Rathore23-Dec-15 20:19
Bhanu Pratap Singh Rathore23-Dec-15 20:19 
GeneralRe: Getting data from tags having specific attribute values Pin
Member 1025370128-Dec-15 20:55
professionalMember 1025370128-Dec-15 20:55 
GeneralRe: Getting data from tags having specific attribute values Pin
Bhanu Pratap Singh Rathore31-Dec-15 5:57
Bhanu Pratap Singh Rathore31-Dec-15 5:57 
GeneralRe: Getting data from tags having specific attribute values Pin
Member 1025370131-Dec-15 20:31
professionalMember 1025370131-Dec-15 20:31 
GeneralRe: Getting data from tags having specific attribute values Pin
Bhanu Pratap Singh Rathore31-Dec-15 21:52
Bhanu Pratap Singh Rathore31-Dec-15 21:52 
GeneralRe: Getting data from tags having specific attribute values Pin
Member 102537011-Jan-16 13:28
professionalMember 102537011-Jan-16 13:28 
QuestionHelpful Tip Pin
MayurDighe27-May-15 3:38
professionalMayurDighe27-May-15 3:38 
QuestionNice Pin
Santhakumar Munuswamy @ Chennai23-May-15 8:32
professionalSanthakumar Munuswamy @ Chennai23-May-15 8:32 
Thanks for nice one

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.