Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...