导航xml节点;

本文关键字:节点 xml 导航 | 更新日期: 2023-09-27 18:18:35

嗨,我有一个从另一个服务返回的xml数据。它看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<response xmlns="http://test.com/group/application" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Response>
<Response>
<ReturnCode>0</ReturnCode>
<Message>Sucess</Message>
<Data>PRINT 'This is a test #2'</Data>
</Response>
</Response>
</response>

我需要Data, Message和ReturnCode的值。Data(PRINT 'This is a test #2')节点中的值可以是单行或数千行。

我正在使用这个c#代码来获取值

XmlDocument = new XmlDocument();

        string Response = obj.getContent(str, 1, 73810, SHA);

        //Console.WriteLine("Response" + Response);
        xm.LoadXml(Response);

        Console.WriteLine(xm.InnerXml);
        XmlNode oldCd;
        XmlElement root = xm.DocumentElement;
        Console.WriteLine(root.InnerText);
        oldCd = root.SelectSingleNode("/response/Response/Response/ReturnCode/Message/Data/");

static void Main()
    {
        try
        {
            svc obj = new svc();
          ..
            //XmlDocument xm = new XmlDocument();
            string rsp = obj.getContent(..;

            String myEncodedString;
            myEncodedString = obj.XmlDecode(rsp);
            XNamespace xmlns = XNamespace.Get("http://xxxx.com/xxx/xx");
            XDocument doc = XDocument.Parse(myEncodedString);
            Console.WriteLine(obj.Return_Message_Data("ReturnCode", myEncodedString));
            Console.WriteLine(obj.Return_Message_Data("Message", myEncodedString));
            Console.WriteLine(obj.Return_Message_Data("Data", myEncodedString));               
            Console.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            Console.ReadLine();
        }
    }

导航xml节点;

试试这个

XmlDocument xml = new XmlDocument();
xml.LoadXml(myXmlString); //myXmlString is the xml file in string //copying xml to string: string myXmlString = xmldoc.OuterXml.ToString();
XmlNodeList xnList = xml.SelectNodes("/responset[@*]/Response");
foreach (XmlNode xn in xnList)
{
XmlNode response = xn.SelectSingleNode("Response");
if (response != null)
{
string rc = response["ReturnCode"].InnerText;
string msg = example["Message"].InnerText;
string data = example["Data"].InnerText;
}
}