Here i will show you how to read the xml file using Xpath in the C#
Here is the XML file which i have used...
< ? xml version="1.0" encoding="UTF-8"?>(please remove the spaces from above XML File)
< projects>
< project id = "BP001">
< name>Banking Project
< / project>
< project id = "TP001">
< name>Telecommunication Project
< / project>
< project id = "PP001">
< name>Portal Project
< / project>
< / projects>
I have used simple Console Application Which reads this XML File & stores data in the list ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Xml;
namespace ConsoleExamples
{
class readXMLusingXPath
{
static void Main(string[] args)
{
readxml read = new readxml();
read.xmlread();
}
}
class readxml
{
private XmlDocument ReadDoc = new XmlDocument();
string nodename = string.Empty;
static XmlNode node;
List
{
ReadDoc.Load("XMLFile1.xml");
node = ReadDoc.SelectSingleNode("projects");
loadnodes(node);
}
public void loadnodes(XmlNode nd)
{
XmlNodeList nodelist = nd.SelectNodes("project");
foreach (XmlNode node1 in nodelist)
{
if (node1.ToString() != null)
{
strnodes.Add(node1.Name.ToString() + " id = " + node1.Attributes.GetNamedItem("id").Value);
XmlNode node2 = node1.SelectSingleNode("name");
if (node2.ToString() != null)
{
strnodes.Add(node2.Name.ToString() + " = " + node1.SelectSingleNode("name").InnerText);
}
}
}
Console.Read();
}
}
}
Hope it helps ....
0 comments:
Post a Comment