Read data from XML with Multiple or Nested Tables in C#

.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;
 
                                            }
                                         }
                                    }
                                }
                            }
                        }
                    }
                }
            }

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.

4 thoughts on “Read data from XML with Multiple or Nested Tables in C#”

  1. 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

Comments are closed.