none
Getting data from "sub nodes" in an XML document RRS feed

  • Question

  • Hi,

    I have an XML document that looks like list

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
    <getRS xmlns="http://webserviceURL">
    <getRSRow>
     <cORR>
      <cType>DBL</cType>
      <nType>DOUBLE</nType>
      <cFeature>01-WL</cFeature>
      <nFeature>NORM</nFeature>
      <cORRR>
        <cRate>BAR</cRate>
        <nRate>BAR</nRate>
        <tRate>G</tRate>
        <beginDate>05/04/2016</beginDate>
        <endDate>31/12/2016</endDate>
        <currency>EUR</currency>
        <SSS>S</SSS>
        <bCB>BB</bCB>
        <maximumA>2</maximumA>
        <minimumA>2</minimumA>
        <maximumC>1</maximumC>
        <maximumI>1</maximumI>
        <maximumP>3</maximumP>
        <rPaxes/>
          <cOBRR>
            <boardCode>BB</boardCode>
            <boardName>option1</boardName>
          </cOBRR>
      </cORRR>
    </cORR>
    ....etc
    </getRSRow>

    and I am trying to get the data from nodes <cORRR> and <cOBRR> found under that.

    The Code I have been trying is

    XmlNodeList elemList = xmlDoc.GetElementsByTagName("cORR"); for (int s = 0; s < elemList.Count; s++) { foreach (XmlNode data in elemList) { string cType = rmdata["cType"].InnerText; textBox1.AppendText("Code: " + cType + Environment.NewLine); string nType = rmdata["nType"].InnerText; textBox1.AppendText("Name: " + nType + Environment.NewLine); string cFeature = rmdata["cFeature"].InnerText; textBox1.AppendText("Feature Code: " + cFeature + Environment.NewLine); string nFeature = rmdata["nFeature"].InnerText; textBox1.AppendText("Feature Name: " + nFeature + Environment.NewLine); /// ---> from here XmlNodeList subelemList1 = rmdata.SelectNodes("/cORRR/cRate"); int count = subelemList1.Count; MessageBox.Show(count.ToString()); foreach (XmlNode data in subelemList1) { string codeRate = data["cRate"].InnerText; textBox1.AppendText("Code: " + cRate + Environment.NewLine);

    etc... } label1.Text = "Saved..."; } }

    but, when it gets to that code, the message box show 0, and it skips to the next iteration of elemList.

    Please, can someone show me what I need to do to get the data from the nodes under <cORRR> and <cOBRR>, for each <cORR></cORR> (Nb: sorry about the naming of the nodes, but this is beyond my control) so the data comes out looking like:

    Code: DBL
    Name: DOUBLE
    Feature Code: 01-WL
    Feature Name: NORM
    Code: BAR
    Code name: BAR
    Rate: G
    
    etc

    for each instance?

    Thank you

    Friday, February 23, 2018 1:01 PM

All replies

  • As it's a SOAP result, why reinventing the wheel? Why not using the build-in service references? This will create all necessary wrappers automatically and you'll get a object based access to the data.

    Otherwise deserialize it yourself.

    Or at least use LINQ/XDocument, cause the structure is fixed..

    Friday, February 23, 2018 1:28 PM
  • Hello G-Oker,

    The issues is occurred by xml namespace name, you need to use the prefix (namespace name) to qualify element names in your path expressions you pass to SelectNodes calls. Completed code like below.

            static void Main(string[] args)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("../../XMLFile1.xml");
    
                XmlNodeList elemList = xmlDoc.GetElementsByTagName("cORR");
    
                for (int s = 0; s < elemList.Count; s++)
                {
    
                    string cType = elemList[s]["cType"].InnerText;
                    Console.WriteLine("Code: " + cType + Environment.NewLine);
    
                    string nType = elemList[s]["nType"].InnerText;
                    Console.WriteLine("Name: " + nType + Environment.NewLine);
    
                    string cFeature = elemList[s]["cFeature"].InnerText;
                    Console.WriteLine("Feature Code: " + cFeature + Environment.NewLine);
    
                    string nFeature = elemList[s]["nFeature"].InnerText;
                    Console.WriteLine("Feature Name: " + nFeature + Environment.NewLine);
    
                    /// ---> from here
                   var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                   nsmgr.AddNamespace("a", "http://webserviceURL");
                   XmlNodeList subelemList1 = elemList[s].SelectNodes("//a:cORR/a:cORRR",nsmgr);
    
                    for (int j = 0; j < subelemList1.Count; j++) {
    
                       string cRate= subelemList1[j]["cRate"].InnerText;
                        Console.WriteLine("Code: " + cRate + Environment.NewLine);
    
                        string nRate = subelemList1[j]["nRate"].InnerText;
                        Console.WriteLine("Code name: " + cRate + Environment.NewLine);
    
                        string tRate = subelemList1[j]["tRate"].InnerText;
                        Console.WriteLine("rate: " + tRate + Environment.NewLine);
                    }
    
                }
            }

    Result.

    Best regards,

    Neil Hu


    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.

    Saturday, February 24, 2018 7:59 AM
    Moderator
  • Hi Fei,

    That you for your help. it's 99% there, but, when I run the code, I get your result, plus 11 more. it's going through the whole document doing the cORRR subnodes.

    could this because of the     for (int j = 0; j < subelemList1.Count; j++)   loop ?

    Saturday, February 24, 2018 9:43 AM
  • >>could this because of the     for (int j = 0; j < subelemList1.Count; j++)   loop ?

    I think so. The statement "elemList[s].SelectNodes " will return all subnodes whose name is "cORRR". If you want to return single node you could try to use SelectSingleNode.


    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.

    Saturday, February 24, 2018 9:58 AM
    Moderator
  • hi, I've changed that but get

    Cannot implicitly convert type System.XML.XMLNode to System.XML.XMLNodeList.

    If I change XmlNodeList to XmlNode, this error goes, the For loop they say

    Operator "<" cannot be applied to operands of type "int" and "method group"

    if I remove the for loop and replace subelemList1[j] with subelemList1[s], I get told

    cannot convert int to string


    • Edited by G-Oker Saturday, February 24, 2018 10:17 AM
    Saturday, February 24, 2018 10:13 AM
  • It's easy to fix. The XmlNodeList is a collection that contains some XmlNode. When you use SelectSingleNode you don't need to use "for" statement. The result like below.

    ...
     var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
     nsmgr.AddNamespace("a", "http://webserviceURL");
     var subelemList1 = elemList[s].SelectSingleNode("//a:cORR/a:cORRR",nsmgr);
     // elemList[s].SelectSingleNode();
                   
    
     string cRate= subelemList1["cRate"].InnerText;
     Console.WriteLine("Code: " + cRate + Environment.NewLine);
    
     string nRate = subelemList1["nRate"].InnerText;
     Console.WriteLine("Code name: " + cRate + Environment.NewLine);
    
     string tRate = subelemList1["tRate"].InnerText;
     Console.WriteLine("rate: " + tRate + Environment.NewLine);
    
    ...

    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.

    Saturday, February 24, 2018 10:27 AM
    Moderator
  • Hi,

    this is now just showing the first cORRR subnode in all of the

    foreach (XmlNode data in elemList) data (instead of there own).

    what have I done wrong?

    Saturday, February 24, 2018 10:40 AM
  • Hello G-Oker,

    The issue is caused by the XPath string. Try the below code.

    ...

    var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("a", "http://webserviceURL"); var subelemList1 = elemList[s].SelectSingleNode(@".//a:cORRR",nsmgr);

    ...

    https://stackoverflow.com/questions/16650598/xmlnode-selectsinglenode-always-returns-same-value-even-though-the-node-changes


    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.

    Sunday, February 25, 2018 5:18 AM
    Moderator
  • Hello again, 

    OK, that seems to have worked, but my code seems to do every 12 times (ie: how many listing thee are in the XML file).

    I am assuming this these lines causing the issue 

    XmlNodeList elemList = xmlDoc.GetElementsByTagName("cORR");
    
    for (int s = 0; s < elemList.Count; s++)
    {

    I think I have sorted it by removing the FOR loop and adding

    XmlNodeList elemList = xmlDoc.GetElementsByTagName("cORRow");
    int s = elemList.Count;
    
    var j = 0;
    foreach (XmlNode rmdata in elemList)
    {
      ..loop thought the nodes / subnodes
    
    
      if (j!=s)
       { j++; }
    }

    IS this "best practice", or is there a better way?


    • Edited by G-Oker Sunday, February 25, 2018 10:28 AM
    Sunday, February 25, 2018 10:02 AM
  • Hello G-Oker,

    Sorry for the delay reply.

    The two pieces of code you provided is the same. What do you mean "my code seems to do every 12 times "? If possible you could give me the entire xml file and result you want to output. I assume the issue is caused by GetElementsByTagName. Of course you should learn how to debug programs and check each variables is correct.

    https://msdn.microsoft.com/en-us/library/mt243867.aspx

    I'm looking forward to your reply.

    Best regards,

    Neil Hu


    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.

    Monday, February 26, 2018 10:35 AM
    Moderator