locked
Reading XML File RRS feed

  • Question

  • User-614943948 posted

    I have an XML File like this

    <?xml version="1.0" encoding="utf-8" ?> 
    - <LS_REGISTER>
      <operation>query</operation> 
      <BuildStatus>1112</BuildStatus> 
      <Status>1110</Status> 
      <LicenseType>3</LicenseType> 
      <DaysToExpire>-32</DaysToExpire> 
      <AppID>1</AppID> 
      <Productid>1</Productid> 
      <LatestVersion>4.00.0023</LatestVersion> 
      <DownloadURL>https://mywebsite/dloadsv/download1.aspx?lang=1033&OEM=1000</DownloadURL> 
      <ErrorCode>200</ErrorCode> 
      </LS_REGISTER>


    Now I want each and every element along with its value to be stored in a C# List, how can I accomplish this. There could be cases that the elements might change. Therefore the user might be sometimes, unaware, of the elements present in the XML File

    Thursday, April 16, 2015 8:07 AM

Answers

  • User2047667423 posted

    Hi please refer the following link it might be helpfull.

    https://support.microsoft.com/en-us/kb/307548

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 16, 2015 8:42 AM
  • User1644755831 posted

    Hello Maverick786us,

    You can create a recursive method that will iterate through all the nodes. For example. (if you have nodes of only level 1 you can simply this by removing recursive logic)

    public static class XmlDocumentExtensions
    {
         
        public static void IterateThroughAllNodes(this XmlDocument doc, Action<XmlNode> elementVisitor)
        {
            if (doc != null && elementVisitor != null)
            {
                //loop through all the childs
                foreach (XmlNode node in doc.ChildNodes)
                {
                    doIterateNode(node, elementVisitor);
                }
            }
        }
    
        private static void doIterateNode(XmlNode node, Action<XmlNode> elementVisitor)
        {
            elementVisitor(node); //fire the event
    
            foreach (XmlNode childNode in node.ChildNodes)
            {
                doIterateNode(childNode, elementVisitor); //more child so do this again
            }
        }
    }

    Now you can use it like this

    var doc = new XmlDocument();
    doc.Load(somePath); //path to your xml doc
    
    Dictionary<string,string> dic = new Dictionary<string,string>();
    doc.IterateThroughAllNodes(
        delegate(XmlNode node)
        {
            // ...Do something with the node...
            dic.Add(node.Name,node.InnerText);
        });
    

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 17, 2015 2:08 AM

All replies

  • User2047667423 posted

    Hi please refer the following link it might be helpfull.

    https://support.microsoft.com/en-us/kb/307548

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 16, 2015 8:42 AM
  • User1644755831 posted

    Hello Maverick786us,

    You can create a recursive method that will iterate through all the nodes. For example. (if you have nodes of only level 1 you can simply this by removing recursive logic)

    public static class XmlDocumentExtensions
    {
         
        public static void IterateThroughAllNodes(this XmlDocument doc, Action<XmlNode> elementVisitor)
        {
            if (doc != null && elementVisitor != null)
            {
                //loop through all the childs
                foreach (XmlNode node in doc.ChildNodes)
                {
                    doIterateNode(node, elementVisitor);
                }
            }
        }
    
        private static void doIterateNode(XmlNode node, Action<XmlNode> elementVisitor)
        {
            elementVisitor(node); //fire the event
    
            foreach (XmlNode childNode in node.ChildNodes)
            {
                doIterateNode(childNode, elementVisitor); //more child so do this again
            }
        }
    }

    Now you can use it like this

    var doc = new XmlDocument();
    doc.Load(somePath); //path to your xml doc
    
    Dictionary<string,string> dic = new Dictionary<string,string>();
    doc.IterateThroughAllNodes(
        delegate(XmlNode node)
        {
            // ...Do something with the node...
            dic.Add(node.Name,node.InnerText);
        });
    

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 17, 2015 2:08 AM