如何在 Windows Phone 7 中解析 XML(数据集)

2024-05-02

我有一个如下所示的 XML,但我无法解析它。请帮我解析下面的 XML。

<?xml version="1.0" encoding="utf-8"?><soap:Envelope     
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"     
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
<GetResponse xmlns="http://tempuri.org/"><GetResult>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<a>hi1</a>
<b>hi2</b>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<a>hi3</a>
<b>hi4</b>
</Table>
</NewDataSet>
</diffgr:diffgram>
</GetResponse></GetResult>
</soap:Body>
</soap:Envelope>

这里我想要表(即 a,b )标签的结果。我尝试使用 Linq 但无法解析它。我尝试了这样的代码:

//XML will be there in response string
String response = e.response;
public static String myNamespace = "http://tempuri.org/";
XDocument reader = XDocument.Parse(response);
var results = from result in reader.Descendants(XName.Get("GetResponse", myNamespace))
              select result.Element("GetResult").

但这段代码返回 null。

EDIT:我使用下面的代码: 字符串响应 = e.response;

public static String myNamespace = "http://tempuri.org/";
XDocument reader = XDocument.Parse(response);
XElement resultElement = reader.Descendants(XName.Get("GetResult", myNamespace)).Single(); 
XElement resultElement1 = resultElement.Descendants(XName.Get("NewDataSet", "")).Single();

所以现在 resultElement1 我得到如下 XML:

<NewDataSet xmlns="">
  <Table diffgr:id="Table1" msdata:rowOrder="0" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<a>hi1</a>
<b>hi2</b>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<a>hi3</a>
<b>hi4</b>
</Table>
</NewDataSet>

那么现在我如何找到 tag 和 tag 的值?

提前致谢。


是什么阻碍了你?

你可以试试这个:

        var resultNewDataSet = XElement.Parse(xmlContent);

        var result = resultNewDataSet.Descendants("Table")
            .Select(t => new
                {
                    aaa = t.Descendants("aaa").First().Value,
                    bbb = t.Descendants("bbb").First().Value
                });

        foreach (var res in result)
        {
            System.Diagnostics.Debug.WriteLine("aaa: " + res.aaa + " / bbb: " + res.bbb);
        }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Windows Phone 7 中解析 XML(数据集) 的相关文章

随机推荐