Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read single node value from xml file

Hi i am trying to get value from xml but it shows node null.

Here is my xml file.

<?xml version="1.0" encoding="utf-8"?>
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.cfhdocmail.com/TestAPI2/Result.xsd https://www.cfhdocmail.com/TestAPI2/Result.xsd" xmlns="https://www.cfhdocmail.com/TestAPI2/Result.xsd">
  <data>
    <key>MailingGUID</key>
    <value>0aa2b2e3-7afa-4002-ab2f-9eb4cbe33ae7</value>
  </data>
  <data>
    <key>OrderRef</key>
    <value>52186</value>
  </data>
</result>

I want to get "MailingGUID" value.

Here is the code that i have tried:

  private void readXML()
    {
        XmlDocument xml = new XmlDocument();
        // You'll need to put the correct path to your xml file here
        xml.Load(Server.MapPath("~/XmlFile11.xml"));

        // Select a specific node
        XmlNode node = xml.SelectSingleNode("result/data/value");
        // Get its value
        string name = node.InnerText;


    }

Please tell me how i can get MailingGUID value.

Thanks

like image 365
deepika Avatar asked Aug 15 '13 10:08

deepika


1 Answers

UPDATE: I think there might be something wrong with your schemas, I removed references to them and your code worked fine. I tried this:

const string str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><result><data><key>MailingGUID</key><value>0aa2b2e3-7afa-4002-ab2f-9eb4cbe33ae7</value></data><data><key>OrderRef</key><value>52186</value></data></result>";
var xml = new XmlDocument();
xml.LoadXml(str);
xml.DocumentElement.SelectSingleNode("/result/data/value").InnerText
like image 120
Marcus Avatar answered Oct 07 '22 13:10

Marcus