read xml file in c#
//Your xml file structure like
following
<MyRootNodes>
<MyRootNode Data0="0">
<Data1>11</Data1>
<Data2>22</Data2>
<Data3>33</Data3>
<Data4>44</Data4>
<Data5>55</Data5>
</MyRootNode>
<MyRootNode Data0="00">
<Data1>1111</Data1>
<Data2>2222</Data2>
<Data3>3333</Data3>
<Data4>4444</Data4>
<Data5>5555</Data5>
</MyRootNode>
<MyRootNode Data0="000">
<Data1>111</Data1>
<Data2>222</Data2>
<Data3>333</Data3>
<Data4>444</Data4>
<Data5>555</Data5>
</MyRootNode>
</MyRootNodes>
Add Namespace
using System.Xml;
Copy following method in your code
behind file
public void
ReadXmlFile()
{
string Path = "
";// Set path local or server
XmlDocument MyDoc = new
XmlDocument();
MyDoc.Load(Path);
XmlElement RootElement = MyDoc.DocumentElement;
XmlNodeList AllNondes = RootElement.SelectNodes("//MyRootNodes");
for (int i =
AllNondes.Count - 1; i >= 0; i--)
{
XmlNode Node = AllNondes[i];
XmlNodeList NodeList = AllNondes[i].SelectNodes("//MyRootNode");
for (int k =
NodeList.Count - 1; k >= 0; k--)
{
XmlNode ChildNode = NodeList[k];
string Name = ChildNode.Attributes[0].Value;
decimal Data1 = Convert.ToDecimal(ChildNode["Data1"].InnerText);
decimal Data2 = Convert.ToDecimal(ChildNode["Data2"].InnerText);
decimal Data3 = Convert.ToDecimal(ChildNode["Data3"].InnerText);
decimal Data4 = Convert.ToDecimal(ChildNode["Data4"].InnerText);
decimal Data5 = Convert.ToDecimal(ChildNode["Data5"].InnerText);
//Here all data in your xml file get Data1,Data2 .....
Variable
}
}
}
No comments:
Post a Comment