In this article I will explain with an example, how to read (parse) XML from URL in ASP.Net using C# and VB.Net.
XML File URL
The following XML File will be used in this article.
<?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:
DataList – For displaying data.
Templates
HeaderTemplate – The content of this template will not be repeated and will be placed in the top most position i.e. head section of the DataList control.
ItemTemplate – The content of this template will be repeated for each record present in its DataSource.
FooterTemplate – The content of this template will not be repeated and will be placed in the bottom most position i.e. footer section of the DataList control.
Controls
ItemTemplate consists of three Label control for displaying CustomerId, Name and Country of Customers.
<asp:DataList ID="dlCustomers" runat="server" >
<HeaderTemplate>
<table class="Grid">
<tr>
<th>Customer Id</th>
<th>Name</th>
<th>Country</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" Text='<%# Eval("CustomerId") %>'></asp:Label></td>
<td><asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label></td>
<td><asp:Label runat="server" Text='<%# Eval("Country") %>'></asp:Label></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
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 Security Protocol is set.
Then, the contents of XML file are downloaded from URL using DownloadString method of the WebClient class.
The DataSet object is created and the XML string is read using StringReader class and converted to DataSet using ReadXml method.
Finally, the DataSet is assigned to the DataSource property of the DataList and the DataList is populated.
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);
dlCustomers.DataSource = ds;
dlCustomers.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.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)
dlCustomers.DataSource = ds
dlCustomers.DataBind()
End Using
End Using
End If
End Sub
Screenshot
Demo
Downloads