Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml SelectSingleNode不返回任何内容_Xml_Xpath - Fatal编程技术网

Xml SelectSingleNode不返回任何内容

Xml SelectSingleNode不返回任何内容,xml,xpath,Xml,Xpath,我正在测试SelectSingleNode函数,以从Visual Studio中的XMLNode对象获取单个节点,如下所示: Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable) nsmgr.AddNamespace(ndListItems.Prefix, ndListItems.NamespaceURI) Dim dummy As XmlNode

我正在测试SelectSingleNode函数,以从Visual Studio中的XMLNode对象获取单个节点,如下所示:

        Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable)
        nsmgr.AddNamespace(ndListItems.Prefix, ndListItems.NamespaceURI)
        Dim dummy As XmlNode = ndListItems.SelectSingleNode("/listitems", nsmgr)
此时,我只是尝试获取根节点,并使用prefix和namespaceURI属性添加到XmlNamespaceManager。问题是,当我运行调试器时,虚拟变量并没有被赋值,也就是说它是空的。请注意,当我分析Prefix和namespace属性的值时,它们如下所示,Prefix=和NamespaceURI=http://schemas.microsoft.com/sharepoint/soap

更新:

尝试更改代码,但我的虚拟XMLNode仍未设置

Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable)
        nsmgr.AddNamespace(ndListItems.Prefix, ndListItems.NamespaceURI)
        Dim dummy As XmlNode = ndListItems.SelectSingleNode("/" + ndListItems.Prefix + "listitems", nsmgr)
下面是我试图获取的XML代码片段,我的最终目标是访问z:row节点的属性

<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<rs:data ItemCount="1">
   <z:row ows_Title="Newhire" ows_FirstName="Rick" ows_WorkPhone="954" ows_HomePhone="954" ows_Email="genny.maxwell@email.com" ows_UserID="Rick.Newhire" ows_MetaInfo="9;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="9" ows_owshiddenversion="1" ows_UniqueId="9;#{0F6251A9-D3B8-4B07-A5F8-23BAF5F2237E}" ows_FSObjType="9;#0" ows_Created="2010-08-18 15:56:40" ows_FileRef="9;#Lists/NewHires/9_.000" />
</rs:data>
</listitems>
显然,将名称空间添加到名称空间管理器时存在问题

此外,XPath表达式最多只能选择顶部元素,但您需要z:row

一种避免名称空间处理的简单且不太优雅的方法如下

使用:


在输入示例中,z:row元素的QName元组是RowsetSchema,row,z。这意味着z前缀的名称空间URI是RowsetSchema

如果我没有弄错您的C代码,那么这个ndListItems.Prefix将计算为listitems元素的前缀,它的前缀为none或none。因此,当您说/+ndListItems.Prefix+listitems时,它将被计算为/listitems,它将被解释为不带名称空间的listitems

因此,我认为您需要:

Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable) 
nsmgr.AddNamespace("soap", "http://schemas.microsoft.com/sharepoint/soap/")
nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset")
nsmgr.AddNamespace("z", "#RowsetSchema")
Dim dummy As XmlNode = ndListItems.SelectSingleNode("/soap:listitems/rs:data/z:row", nsmgr)

编辑:在我发布之后,我想到了这个问题。

您应该展示您的XML片段,以及如何创建XmlDocumentGood问题+1。请参阅我的答案,了解问题的解释和解决方案。嘿,迪米特,谢谢你回复我。我尝试了上面的代码片段,但在我的伪变量中仍然一无所获。我将上面的代码和XML片段发布为Streloksuggested@kingrichard2005:我完全重新编写了一个完全不使用名称空间的解决方案。您必须学习更多有关使用名称空间的知识,但是这个主题无法用一个问题来解释。仍然没有被分配。我在MSDN上找到了一篇关于XML名称空间的文章,希望这篇文章能澄清一些问题。我不确定为什么它仍然没有被分配,我尝试了两种解决方案。@kingrichard2005:我可以向您保证,这些表达式选择了所需的节点——就XPath而言,这些节点没有问题——c代码中一定有奇怪的东西。
SelectSingleNode("/*/*[local-name()='data']/*[local-name()='row']")
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable) 
nsmgr.AddNamespace("soap", "http://schemas.microsoft.com/sharepoint/soap/")
nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset")
nsmgr.AddNamespace("z", "#RowsetSchema")
Dim dummy As XmlNode = ndListItems.SelectSingleNode("/soap:listitems/rs:data/z:row", nsmgr)