Saturday, 19 January 2013 13:56

how to read from a xml file

Written by 
Rate this item
(0 votes)
suppose you have some data in the xml file as shown -

<?xml version="1.0" encoding="utf-8" ?>
<system>
<version>1.0.0.0</version>
<url>http://yoursite.com/download/1.0.0.0/</url>
</system>

if you want to read those data according to element
then following codes surely help you.there are many
ways to read from a xml file.this is a way to read -

string xmlURL = "http://yoursite.com/info.xml";
reader = new XmlTextReader(xmlURL);
reader.MoveToContent();
string elementName = "";

if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "system"))
{
while (reader.Read())
{
// when we find an element node,
// we remember its name
if (reader.NodeType == XmlNodeType.Element)
elementName = reader.Name;
else
{
// for text nodes...
if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
{
// we check what the name of the node was
switch (elementName)
{
case "version":
NewVersion = new Version(reader.Value);
break;
case "url":
url = reader.Value;
break;
}
}
}
}
}

Read 1721 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments