locked
Reading advance XML file RRS feed

  • Question

  • User-1103262355 posted

    Hi all,

    I have one xml file from one vendor but the file looks very huge having lot of nodes. If i will make loop through each loop i have to write the code may be more than the xml file data Yell

    Is there anyway to ready all nodes automatically.... I am attaching the small peace of that file.

    <Psw xmlns="http://localhost">
      <exid>20</exid>
      <Mes>
        <Me>
          <doc>
            <ps>
              <ghder>
                <MId>75755550cc</MId>
                <cD>2010-12-08 10:27:01</cD>
                <tinfo>
                  <smth>BHND</smth>
                  <CCN>
                    <pn>
                      GHI</Pn>
                    </CCN>
                </tinfo>
                <dac>
                  <Id>
                    <pct>
                      <Id>7843y74303443</Id>
                    </pct>
                  </Id>
                </dac>
              </ghder>
            </ps>
          </doc>
        </Me>
      </Mes>
    </Psw>


    I tried using LINQ but i am only able to read node by node with loop for each node.


    Thank You

    Monday, December 27, 2010 12:51 PM

Answers

  • User-967720686 posted

    Hi,

    Try the following code.

                XmlDocument document = new XmlDocument();
                document.Load(@"C:\Temp\Test.xml");
                List<KeyValuePair<string, string>> valueList = new List<KeyValuePair<string, string>>();
                ParseNodes(valueList, document.ChildNodes);
    
                foreach (KeyValuePair<string, string> p in valueList)
                {
                    Console.WriteLine("Key: {0}, Value: {1}", p.Key, p.Value);
                }
    
    
            public static void ParseNodes(List<KeyValuePair<string, string>> valueList, XmlNodeList nodeList)
            {
                foreach (System.Xml.XmlNode n in nodeList)
                {
                    if (n.ChildNodes.Count == 0)
                    {
                        valueList.Add(new KeyValuePair<string, string>(n.ParentNode.Name, n.InnerText));
                    }
                    else
                    {
                        ParseNodes(valueList, n.ChildNodes);
                    }
                }
            }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 27, 2010 6:33 PM
  • User3866881 posted

    Hey, This is the LINQ:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    namespace ConsoleApplication1
    {
        class Program
        {
            static Dictionary<string, string> keyvalues = new Dictionary<string, string>();

            static void ReadNestNode(XDocument doc, XElement element)
            {
                if (element.HasElements)
                {
                    foreach (var item in element.Elements())
                    {
                        ReadNestNode(doc, item);
                    }
                }
                else
                {

                        keyvalues.Add(element.Name.LocalName, element.Value.Trim());
                        return;
                }
            }

            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load("XMLFile1.xml");
                ReadNestNode(doc, doc.Root);
            
                foreach (KeyValuePair<string,string> item in keyvalues)
                {
                    Console.WriteLine(item.Key+"<---->"+item.Value);
                }
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 28, 2010 9:33 PM

All replies

  • User-967720686 posted

    Hi,

    Try the following code.

                XmlDocument document = new XmlDocument();
                document.Load(@"C:\Temp\Test.xml");
                List<KeyValuePair<string, string>> valueList = new List<KeyValuePair<string, string>>();
                ParseNodes(valueList, document.ChildNodes);
    
                foreach (KeyValuePair<string, string> p in valueList)
                {
                    Console.WriteLine("Key: {0}, Value: {1}", p.Key, p.Value);
                }
    
    
            public static void ParseNodes(List<KeyValuePair<string, string>> valueList, XmlNodeList nodeList)
            {
                foreach (System.Xml.XmlNode n in nodeList)
                {
                    if (n.ChildNodes.Count == 0)
                    {
                        valueList.Add(new KeyValuePair<string, string>(n.ParentNode.Name, n.InnerText));
                    }
                    else
                    {
                        ParseNodes(valueList, n.ChildNodes);
                    }
                }
            }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 27, 2010 6:33 PM
  • User3866881 posted

    Hey, This is the LINQ:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    namespace ConsoleApplication1
    {
        class Program
        {
            static Dictionary<string, string> keyvalues = new Dictionary<string, string>();

            static void ReadNestNode(XDocument doc, XElement element)
            {
                if (element.HasElements)
                {
                    foreach (var item in element.Elements())
                    {
                        ReadNestNode(doc, item);
                    }
                }
                else
                {

                        keyvalues.Add(element.Name.LocalName, element.Value.Trim());
                        return;
                }
            }

            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load("XMLFile1.xml");
                ReadNestNode(doc, doc.Root);
            
                foreach (KeyValuePair<string,string> item in keyvalues)
                {
                    Console.WriteLine(item.Key+"<---->"+item.Value);
                }
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 28, 2010 9:33 PM