0

我尝试了几种方法,从 Linq 到将数据加载到 XML 文档,但我似乎无法返回我需要的结果。

这是示例 XML:

<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common" xmlns:event="http://www.webex.com/schemas/2002/06/service/event"><serv:header><serv:response><serv:result>SUCCESS</serv:result><serv:gsbStatus>PRIMARY</serv:gsbStatus></serv:response></serv:header><serv:body><serv:bodyContent xsi:type="event:createEventResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><event:sessionKey>11111111</event:sessionKey><event:guestToken>111111111111111111111</event:guestToken></serv:bodyContent></serv:body></serv:message>

而且,这就是我试图做的:

StreamReader reader = new StreamReader(dataStream);
XmlDocument doc = new XmlDocument();
doc.LoadXml(reader.ReadToEnd());
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
XmlNamespaceManager ns2 = new XmlNamespaceManager(doc.NameTable);
XmlNamespaceManager ns3 = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("serv", "http://www.webex.com/schemas/2002/06/service");
ns2.AddNamespace("com", "http://www.webex.com/schemas/2002/06/common");
ns3.AddNamespace("event", "http://www.webex.com/schemas/2002/06/service/event");
XmlNode node = doc.SelectSingleNode("result",ns);

然而,由于某种原因,我似乎永远无法返回实际结果,根据上面的实际 xml,它应该是“成功”或“失败”。

我怎样才能做到这一点?

4

2 回答 2

2

您的 xpath 查询不正确。

试试这个:

XmlNode node = doc.SelectSingleNode("//serv:result",ns);

或者

XmlNode node = doc.SelectSingleNode("serv:message/serv:header/serv:response/serv:result",ns);
于 2011-02-08T23:24:18.933 回答
0

这有效:

XDocument doc = XDocument.Load(@"test.xml");
XNamespace serv = "http://www.webex.com/schemas/2002/06/service";
var result = doc.Descendants(serv + "result").First().Value;
于 2011-02-08T23:24:24.207 回答