【发布时间】:2010-08-20 10:02:48
【问题描述】:
我正在查询 Sharepoint 服务器端并将结果作为 Xml 返回。在通过 WebMethod 将其发送到 jQuery 之前,我想将 Xml 精简为更轻量级的东西。
但是我的 XPath 查询不起作用。我认为下面的代码会返回所有 Document 节点,但它什么也不返回。我之前使用过 XPath,我认为 //Document 可以解决问题。
C# XPath 查询
XmlDocument xmlResults = new XmlDocument();
xmlResults.LoadXml(xml); // XML is a string containing the XML source shown below
XmlNodeList results = xmlResults.SelectNodes("//Document");
正在查询的 XML
<ResponsePacket xmlns="urn:Microsoft.Search.Response">
<Response domain="QDomain">
<Range>
<StartAt>1</StartAt>
<Count>2</Count>
<TotalAvailable>2</TotalAvailable>
<Results>
<Document relevance="126" xmlns="urn:Microsoft.Search.Response.Document">
<Title>Example 1.doc</Title>
<Action>
<LinkUrl size="32256" fileExt="doc">http://hqiis99/Mercury/Mercury documents/Example 1.doc</LinkUrl>
</Action>
<Description />
<Date>2010-08-19T14:44:56+01:00</Date>
</Document>
<Document relevance="31" xmlns="urn:Microsoft.Search.Response.Document">
<Title>Mercury documents</Title>
<Action>
<LinkUrl size="0" fileExt="aspx">http://hqiis99/mercury/Mercury documents/Forms/AllItems.aspx</LinkUrl>
</Action>
<Description />
<Date>2010-08-19T14:49:39+01:00</Date>
</Document>
</Results>
</Range>
<Status>SUCCESS</Status>
</Response>
</ResponsePacket>
【问题讨论】:
-
XPath 将返回源 XML 文件中的所有
Document节点。你确定它们在那里吗?如果是,你确定你的results变量是空的吗? -
xml 字符串变量包含上面显示的 XML,我已经更新了我的源代码并添加了注释以传达这一点