locked
Get root and child node values from XML files RRS feed

  • Question

  • User1793652459 posted

    Hello

    I have a XML file that's in the format shown below and I need to extract the values example id (001), type (customer) and RegDate(2018-01-24T12:35:58) from it.  Format will be the same but these 3 values will change for different files.  I guess cf:Comp is root and cf:comment is a node or something, I have tried different codes but can't get a handle to these values. This file format is different then the examples I have seen on web and can't find something that will work.

    XML file example: 

    <?xml version="1.0" encoding="utf-8"?>
    <cf:Comp xmlns:cf="http://wwws.xmllope/" id = "001" type ="customer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <cf:comment>
    <tf:FiscalDigital Version="1.1" xsi:schemaLocation="http://www.xx/abc" RegDate="2018-01-24T12:35:58"/>
    </cf:comment>
    </cf:Comp> 

    Code I tried: 

              XmlDocument doc = new XmlDocument();
                doc.Load("C:\\customer.xml");
                XmlNodeList elemList = doc.GetElementsByTagName("cf:Comp");
              for (int i = 0; i < elemList.Count; i++)

                {              Console.WriteLine(elemList[i].InnerXml);       
                }

     Can someone please guide me.  I need to store these 3 values in a SQL table but for now just trying to display them in the Console.  Either C# or VB.NET is fine. 

    Thanks

    Monday, January 29, 2018 4:52 PM

Answers

  • User2103319870 posted

    id (001), type (customer)

    For ID and Type you need to read the value from elements attribute collection like below

      XmlDocument doc = new XmlDocument();
                doc.Load(@"C:\\customer.xml");
                XmlNodeList elemList = doc.GetElementsByTagName("cf:Comp");
                for (int i = 0; i < elemList.Count; i++)
    
                {
                    //Get the id value
                    string id = elemList[i].Attributes["id"].Value;
                    //Get the type value
                    string type = elemList[i].Attributes["type"].Value;
    
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 29, 2018 6:13 PM

All replies

  • User2103319870 posted

    id (001), type (customer)

    For ID and Type you need to read the value from elements attribute collection like below

      XmlDocument doc = new XmlDocument();
                doc.Load(@"C:\\customer.xml");
                XmlNodeList elemList = doc.GetElementsByTagName("cf:Comp");
                for (int i = 0; i < elemList.Count; i++)
    
                {
                    //Get the id value
                    string id = elemList[i].Attributes["id"].Value;
                    //Get the type value
                    string type = elemList[i].Attributes["type"].Value;
    
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 29, 2018 6:13 PM
  • User1793652459 posted

    Thank you, it worked perfectly. 

    Monday, January 29, 2018 7:22 PM