打开肥皂体

时间:2015-06-05 06:49:56

标签: c# xml linq-to-xml

我一直在努力解析xml字符串,但一切都无济于事

<EnquirySingleItemResponse xmlns="http://tempuri.org/">                  <EnquirySingleItemResult>
    <Response>
      <MSG>SUCCESS</MSG>
      <INFO>TESTING</INFO>
    </Response>   </EnquirySingleItemResult> </EnquirySingleItemResponse>

无论我如何解析它,我的代码都会返回null或xml标记中的文本。我查了一些帖子,但似乎没有用。 请参阅下面的代码

 XElement anotherUnwrappedResponse = ( from _xml in axdoc.Descendants(tempuri + "EnquirySingleItemResponse")
                                       select _xml).FirstOrDefault();

        string response = anotherUnwrappedResponse.Value;

使用axdoc.Descendants是因为我解开了肥皂体,上面有xml

1 个答案:

答案 0 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
            "<EnquirySingleItemResponse xmlns=\"http://tempuri.org/\">" +
                "<EnquirySingleItemResult>" +
                "<Response>" +
                  "<MSG>SUCCESS</MSG>" +
                  "<INFO>TESTING</INFO>" +
                "</Response>   </EnquirySingleItemResult> </EnquirySingleItemResponse>";

            XDocument doc = XDocument.Parse(input);
            string msg = doc.Descendants().Where(x => x.Name.LocalName == "MSG").FirstOrDefault().Value;
            string info = doc.Descendants().Where(x => x.Name.LocalName == "INFO").FirstOrDefault().Value;
        }
    }
}
​