Read XML data from a URL by using Visual C#
C# has a powerful and flexible namespace System.Xml to manipulate XML. System.Xml namespace provides the XMLTextReader class to read XML (Extensible Markup Language) from a URL (Uniform Resource Locator).
Now we want to read xml file test.xml (http://localhost/test.xml) located at your localhost. You can also use www.tipsntracks.com site map at "http://www.tipsntracks.com/sitemap.xml". let us look at structure of test.xml, I am going to used.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<urlset>
<url>
<loc>http://www.tipsntracks.com/84/aspdotnet-web-forms-with-data-lock.html</loc>
<lastmod>2008-12-10T18:23:45+00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.2</priority>
</url>
<url>
<loc>http://www.tipsntracks.com/67/xml-at-a-glance.html</loc>
<lastmod>2008-11-03T18:01:00+00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.2</priority>
</url>
<url>
<loc>http://www.tipsntracks.com/69/what-is-a-markup-language.html</loc>
<lastmod>2008-11-03T17:53:29+00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.2</priority>
</url>
<url>
<loc>http://www.tipsntracks.com/73/xml-uses-and-advantage.html</loc>
<lastmod>2008-11-03T17:37:23+00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.2</priority>
</url>
<url>
<loc>http://www.tipsntracks.com/62/how-can-i-register-install-a-dll-on-windows.html</loc>
<lastmod>2008-11-03T16:48:16+00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.2</priority>
</url>
</urlset>
Create a new Visual C# Console Application. Now we need to use System.Xml namespace which provides us the XmlTextReader class. XmlTextReader provides forward-only, read-only access to a stream of XML data. XmlTextReader does not provide data validation. XmlTextReader checks the DTD for well-formedness, but does not validate using the DTD. We need to use System.Xml namespace with the "using directive" like using System.Xml;
Declare a constant for the url, using constant will add more flexibility to our code. we can specify new xml url just by changing value of the constant. Create an instance of the XmlTextReader class, and pass the URL to the constructor. XmlTextReader performs sequential reads to move across the XML data and uses the Read method to obtain the next record. The Read method returns false if there are no more records. The following piece of code will do it for us.
using System;
using System.Text;
using System.Xml;
namespace ReadXML
{
class Program
{
static void Main(string[] args)
{
//String xmlURL = "http://www.tipsntracks.com/sitemap.xml";
String xmlURL = "http://localhost:75/test.xml";
XmlTextReader xmlReader = new XmlTextReader(xmlURL);
while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + xmlReader.Name);
while (xmlReader.MoveToNextAttribute()) // Read the attributes.
Console.Write(" " + xmlReader.Name + "=’" + xmlReader.Value + "’");
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(xmlReader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + xmlReader.Name);
Console.WriteLine(">");
break;
}
}
Console.WriteLine("Press any key to continue…");
Console.ReadLine(); //Pause
}
}
}
To process the XML data, the XmlNodeType property help us to determine each record and his node type. The Name and Value properties return the node name (the element and attribute names) and the node value (the node text) of the current node (or record). To inspect the attributes associated with the nodes; The MovetoNextAttribute method moves sequentially through each attribute in the element. Use the HasAttributes property to test whether the node has any attributes.





Comments
No comments yet.
Leave a comment