none
How to iterate in xml data using XmlDocument class RRS feed

  • Question

  • suppose my sample xml is 

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <Folder>
        <path>C:\Users\bsushil\Documents\Sharpdesk Desktop\Tinki</path>
        <name>Tinkiiiii</name>
      </Folder>
      <Folder>
        <path>C:\Users\bsushil\Documents\Sharpdesk Desktop\Tinki\New folder</path>
        <name>New folder</name>
      </Folder>
      <Folder>
        <path>C:\Users\bsushil\Documents\Sharpdesk Desktop\Tinki\New folder (1)</path>
        <name>New folder (1)</name>
      </Folder>
    </root>

    now tell me how could i iterate in xml data for path and name ?

    i found few good sample url

    https://social.msdn.microsoft.com/Forums/en-US/cf1b9d51-b1d3-49cd-b6b6-cad0c504d899/how-to-loop-through-certain-xml-nodes?forum=csharpgeneral
    https://www.codeproject.com/Questions/1232542/How-to-iterate-XML-file-using-Csharp
    https://answers.unity.com/questions/513582/how-to-iterate-through-every-node-in-xml.html
    https://www.experts-exchange.com/questions/28897804/XML-Loop-through-nodes-via-C.html
    https://forums.asp.net/t/2094931.aspx?Loop+through+XmlNodeList+with+C+
    https://stackoverflow.com/questions/2915294/iterating-through-all-nodes-in-xml-file

    Saturday, November 17, 2018 6:37 PM

Answers

  • Hi Sudip_inn,

    Please try the following sample by using LINQ to xml

    XDocument doc = XDocument.Load("Test.xml");
    
    var paths = doc.Descendants("path").ToList();
    
    foreach (var item in paths)
    {
        Console.WriteLine(item.Value);
    }
    var names = doc.Descendants("name").ToList();
    foreach (var item in names)
    {
        Console.WriteLine(item.Value);
    }
    
    Console.ReadLine();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Monday, November 19, 2018 7:29 PM
    Monday, November 19, 2018 2:33 AM
    Moderator
  • This way it can be done

    XmlDocument file = new XmlDocument();
                file.Load(Server.MapPath("/xml/folder.xml"));//please pass your xml file's 
                         absolute path
               
                XmlNodeList xmlNodeList = file.SelectNodes("//Folder");
    
                foreach (XmlNode child in xmlNodeList)
                {
                    Response.Write("path:" + child["path"].InnerText + "  <br/> ");
                    Response.Write("name:" + child["name"].InnerText+"<br/>");
                }

    • Marked as answer by Sudip_inn Monday, November 19, 2018 7:30 PM
    Monday, November 19, 2018 7:29 PM

All replies

  • Hi Sudip_inn,

    Please try the following sample by using LINQ to xml

    XDocument doc = XDocument.Load("Test.xml");
    
    var paths = doc.Descendants("path").ToList();
    
    foreach (var item in paths)
    {
        Console.WriteLine(item.Value);
    }
    var names = doc.Descendants("name").ToList();
    foreach (var item in names)
    {
        Console.WriteLine(item.Value);
    }
    
    Console.ReadLine();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Monday, November 19, 2018 7:29 PM
    Monday, November 19, 2018 2:33 AM
    Moderator
  • This way it can be done

    XmlDocument file = new XmlDocument();
                file.Load(Server.MapPath("/xml/folder.xml"));//please pass your xml file's 
                         absolute path
               
                XmlNodeList xmlNodeList = file.SelectNodes("//Folder");
    
                foreach (XmlNode child in xmlNodeList)
                {
                    Response.Write("path:" + child["path"].InnerText + "  <br/> ");
                    Response.Write("name:" + child["name"].InnerText+"<br/>");
                }

    • Marked as answer by Sudip_inn Monday, November 19, 2018 7:30 PM
    Monday, November 19, 2018 7:29 PM