In this article I will explain with an example, how to read (parse) XML from URL in ASP.Net using C# and VB.Net.
The XML will be first downloaded from an URL using WebClient class and then will be converted to DataSet.
Finally, the DataSet will be used to populate the GridView control in ASP.Net using C# and VB.Net.
 
 
ASPSnippets Test API
The following URL will be used in this article.
The URL returns the following XML.
<?xml version="1.0" standalone="yes"?>
<Customers>
    <Customer>
       <CustomerId>1</CustomerId>
       <Name>John Hammond</Name>
       <Country>United States</Country>
    </Customer>
    <Customer>
       <CustomerId>2</CustomerId>
       <Name>Mudassar Khan</Name>
       <Country>India</Country>
    </Customer>
    <Customer>
       <CustomerId>3</CustomerId>
       <Name>Suzanne Mathews</Name>
       <Country>France</Country>
    </Customer>
    <Customer>
       <CustomerId>4</CustomerId>
       <Name>Robert Schidner</Name>
        <Country>Russia</Country>
    </Customer>
</Customers>
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net GridView control.
<asp:GridView ID="gvCustomers" runat="server"></asp:GridView>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Data;
using System.IO;
 
VB.Net
Imports System.Net
Imports System.Data
Imports System.IO
 
 
Reading (Parsing) XML from URL in ASP.Net
Inside the Page Load event handler, first the contents of XML file are downloaded from an URL using DownloadString method of the WebClient class.
Then, the XML string is read using StringReader class and converted to DataSet using ReadXml method.
Finally, the DataSet is used to populate the GridView control in ASP.Net using C# and VB.Net.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        string xml = (new WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.xml");
        using (DataSet ds = new DataSet())
        {
            using (StringReader sr = new StringReader(xml))
            {
                ds.ReadXml(sr);
                gvCustomers.DataSource = ds;
                gvCustomers.DataBind();
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) HandlesMe.Load
    If NotMe.IsPostBack Then
        ServicePointManager.Expect100Continue = True
        ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
        Dim xml As String = (New WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.xml")
        Using ds As DataSet = New DataSet()
            Using sr As StringReader = New StringReader(xml)
                ds.ReadXml(sr)
                gvCustomers.DataSource = ds
                gvCustomers.DataBind()
            End Using
        End Using
    End If
End Sub
 
 
Screenshot
Read (Parse) XML from URL in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads