0

I have problem with equal by field ID in xml file and put this data to datagridview in c#. I know how to do this in xslt, but i dont know how to use it in c#. Below is xml file source:

<MAGIC>
  <DOCS>
    <DOC>
      <POS>
        <CUSTOMER_ID>1</CUSTOMER_ID>
      </POS>
    </DOC>
  </DOCS>
  <CUSTOMERS>
    <CUSTOMER>
      <ID>1</ID>
      <NAME>NAME1</NAME>
    </CUSTOMER>
  </CUSTOMERS>
</MAGIC> 

My code to this xml file is like this:

        XmlNodeList xnListPos = xml.SelectNodes("/MAGIC/DOCS/DOC/POS");

        string id = "";
        string nameOfCustomer = "";

        foreach (XmlNode xnpos in xnListPos)
        {
            id = xnpos["CUSTOMER_ID"].InnerText;
            nameOfCustomer = xml.SelectSingleNode("/MAGIC/CUSTOMERS/CUSTOMER[ID=id]/NAME");
            dgv.Rows.Add(id, nameOfCustomer);
        }    

But I have a error System.Xml.XmlNode.SelectSingleNode(...) returned null. I don`t know how it works in c#. Maybe some one can help me.

jeffers
  • 145
  • 3
  • 13
  • 3
    Did you mean `xml.SelectSingleNode($"/MAGIC/CUSTOMERS/CUSTOMER[ID={id}]/NAME").InnerText;`? – Xerillio Jul 11 '21 at 19:58
  • Yes, that line works in xslt variable, but not in c#. I need to get name of customer from customers section. – jeffers Jul 11 '21 at 20:04
  • 1
    What do you mean "but not in c#"? It works just fine [here](https://dotnetfiddle.net/QXQDxu) – Xerillio Jul 11 '21 at 20:08
  • Thanks very much Xerillio, I don`t use token $ in xml path and don`t use {} in id. Everything works fine. Thanks again:) – jeffers Jul 11 '21 at 20:33

0 Answers0