.Net provide us some built in functions to read a XML file but those all will work if our xml file have single table and very simple format.
If we have multiple nested tables(Table within Table) in XML the we need to use C# foreach loop to find out our required data.
The following example show you how can we read data from this type of XML file
string myXMLfile = "D:\\MyXML.xml"; XmlTextReader textReader = new XmlTextReader(myXMLfile); XmlDocument doc = new XmlDocument(); doc.Load(myXMLfile); XmlNodeList List = doc.GetElementsByTagName("RWResponse"); foreach (XmlNode node in List) { XmlNode Element = (XmlNode)node; foreach (XmlNode node1 in Element) { XmlNode Element1 = (XmlNode)node1; foreach (XmlNode node2 in Element1) { XmlNode Element2 = (XmlNode)node2; foreach (XmlNode node3 in Element2) { XmlNode Element3 = (XmlNode)node3; if (Element3.Name.ToUpper() != "HEADER") { if (!Element3.OuterXml.ToString().Contains("ROW type=\"subtotal\"")) { if (!Element3.OuterXml.ToString().Contains("ROW type=\"total\"")) { DataRow dr = ret_XML_Data_in_DataTable.NewRow(); foreach (XmlNode node4 in Element3) { XmlElement Element4 = (XmlElement)node4; } } } } } } } } }
Hello Expert,
Can u help in situation where i have little different requirement ! i have few xls file which contains link based content which navigates to another sheet on click of content.
I need to convert this excel file into xml and want to read this xml in C# asp.net application where i need to show xml content dynamically creating html code ..
Currently i have statically created one xml file which contains node and sub node in the file which i m reading in dataset in code behind and displaying it by created run time html table and binding it to html div. but this does not full fill my requirement as .. few xml file may have nested which is difficult to read in dataset, your above solution is quite similar to my requirement but i have no clue how i can read xml in my own format.
moreover you can suggest if any best alternative available…
Help highly appriciated
Hello Pravin
Surely i will help you. Can you send me your sample xml file so that I can try to do this?
Good Day Expert,
I Still Need Help Please
Please explain your problem.