Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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
选择在vb.net中不工作的节点_Vb.net_Xpath_Xmldocument - Fatal编程技术网

选择在vb.net中不工作的节点

选择在vb.net中不工作的节点,vb.net,xpath,xmldocument,Vb.net,Xpath,Xmldocument,我喜欢xml <author-group id="a001"> <author id="aa001"> <given-name id="g001">Name</given-name> <surname id="s001">Name</surname> </author> <author id="aa002"> <given-name id="g002">Name</give

我喜欢xml

<author-group id="a001">
<author id="aa001">
  <given-name id="g001">Name</given-name>
  <surname id="s001">Name</surname>
</author>
<author id="aa002">
 <given-name id="g002">Name</given-name>
 <surname id="s002">Name</surname>
</author>
<author id="aa003">
   <given-name id="g003">Name</given-name>
   <surname id="s003">Name</surname>
</author>
</author-group>

这里的Cnt值为零,我不知道为什么它不工作。我使用的是UTF-8编码的xml文件。

我将您的代码复制到一个新项目中,它工作正常。以下是我的测试项目中的代码:

Dim XMLStr As String = "<author-group id=""a001""><author id=""aa001"">  <given-name id=""g001"">Name</given-name>  <surname id=""s001"">Name</surname></author><author id=""aa002""> <given-name id=""g002"">Name</given-name> <surname id=""s002"">Name</surname></author><author id=""aa003"">   <given-name id=""g003"">Name</given-name>   <surname id=""s003"">Name</surname></author></author-group>"
Dim xDom As New Xml.XmlDocument
xDom.LoadXml(XMLStr)
Dim Lst As XmlNodeList = xDom.SelectNodes("//author")
Dim Cnt As Integer = Lst.Count()
Dim XMLStr As String=“Name”
Dim xDom作为新的Xml.Xml文档
LoadXml(XMLStr)
Dim Lst作为XmlNodeList=xDom.SelectNodes(“//作者”)
Dim Cnt As Integer=Lst.Count()
因此,您的错误一定在代码中的其他地方。

尝试以下方法(添加xml DTD,并使用XDocument):

Dim x As XDocument=
名称
名称
名称
名称
名称
名称
Dim xDom作为新的XML文档
xDom.LoadXml(x.ToString())
Dim Lst作为XmlNodeList=xDom.SelectNodes(“//作者”)
Dim Cnt As Integer=Lst.Count()

属性定义了所谓的默认名称空间,该名称空间应用于元素所在元素下的所有内容。但是,XPath没有指定任何名称空间,这意味着它将只查找不在任何名称空间中的
author
元素。因此,您实际上是在寻找错误的元素名称

使用名称空间的XPath查询需要提供名称空间前缀到名称空间的映射的
XmlNamespaceManager
。在XPath中也可以使用默认名称空间,如下所示

将以下代码段中的
xxx
替换为默认名称空间URI,并在用文档加载
xDom
后执行此操作:

'Create an XmlNamespaceManager for resolving namespaces.
Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(xDom.NameTable)
nsmgr.AddNamespace(String.Empty, "xxx")

Dim nodeList as XmlNodeList 
Dim root as XmlElement = xDom.DocumentElement
nodeList = root.SelectNodes("//author", nsmgr)

这是整个XML文档,还是仅仅是一个摘录?我想知道文档是否定义了
xmlns
属性的名称空间。不,这不是整个XML文档。是的,它在其他元素中有一些名称空间
Dim x As XDocument = <?xml version="1.0" encoding="utf-8"?>
                             <author-group id="a001">
                                 <author id="aa001">
                                     <given-name id="g001">Name</given-name>
                                     <surname id="s001">Name</surname>
                                 </author>
                                 <author id="aa002">
                                     <given-name id="g002">Name</given-name>
                                     <surname id="s002">Name</surname>
                                 </author>
                                 <author id="aa003">
                                     <given-name id="g003">Name</given-name>
                                     <surname id="s003">Name</surname>
                                 </author>
                             </author-group>

Dim xDom As New XmlDocument
xDom.LoadXml(x.ToString())
Dim Lst As XmlNodeList = xDom.SelectNodes("//author")
Dim Cnt As Integer = Lst.Count()
'Create an XmlNamespaceManager for resolving namespaces.
Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(xDom.NameTable)
nsmgr.AddNamespace(String.Empty, "xxx")

Dim nodeList as XmlNodeList 
Dim root as XmlElement = xDom.DocumentElement
nodeList = root.SelectNodes("//author", nsmgr)