我有一个如下所示的 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 的值?
提前致谢。