首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于子元素属性值的ASP.NET节点选择

基于子元素属性值的ASP.NET节点选择
EN

Stack Overflow用户
提问于 2017-11-03 20:06:09
回答 1查看 597关注 0票数 0

我正在循环下面的XML,并找到所有的entry元素,其中底层category元素具有thr term属性的值collection。下面的例子中有两个。然后,对于这些对象,获取link元素上的link属性的值。然而,我似乎找不到合适的选择器:

代码语言:javascript
运行
复制
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>demo</title>
    <id>urn:uuid:071d9650-ae6c-11e7-8f1a-0800200c9a66</id>
    <link rel="self" href="https://test.com/atom/index.xml"/>
    <updated>2017-10-11T14:37:33+02:00</updated>
    <author>
        <name>Test</name>
        <uri>http://www.test.com</uri>
    </author>
    <generator version="1.8">Agent</generator>
    <entry>
        <title>YDEMO</title>
        <id>urn:uuid:15f44340-ae6c-11e7-8f1a-0800200c9a66</id>
        <category term="collection"/>
        <published>2017-10-11T13:41:53+02:00</published>
        <updated>2017-10-11T14:37:33+02:00</updated>
        <link rel="alternate" href="https://www.myurl.com" type="text/xml"/>
        <mcp:projectScenario xmlns:mcp="http://webservice.yes-co.nl/3mcp/1.5/atom-extension">NBvh</mcp:projectScenario>
    </entry>
    <entry>
        <title>DEMO 2</title>
        <id>urn:uuid:25f44340-ae6c-11e7-8f1a-0800200c9a00</id>
        <category term="collection"/>
        <published>2017-10-11T13:42:53+02:00</published>
        <updated>2017-10-11T14:38:33+02:00</updated>
        <link rel="alternate" href="https://www.myurl2.com" type="text/xml"/>
        <mcp:projectScenario xmlns:mcp="http://webservice.yes-co.nl/3mcp/1.5/atom-extension">BBvh</mcp:projectScenario>
    </entry>
    <entry>
        <title>photo</title>
        <id>12</id>
        <category term="metadata"/>
        <updated>2016-10-11T14:38:33+02:00</updated>
        <link rel="alternate" href="https://www.myurl2.com" type="text/xml"/>
    </entry>
    <entry
        xmlns:mcp="http://webservice.yes-co.nl/3mcp/1.5/atom-extension">
        <title>No title</title>
        <id>urn:uuid:6d65c57f-621f-4c15-8a1d-5dc967423d5d</id>
        <category term="media"/>
        <published>2017-10-11T13:39:43+02:00</published>
        <updated>2017-10-11T13:39:43+02:00</updated>
        <link
            xmlns:mcp="http://webservice.yes-co.nl/3mcp/1.5/atom-extension" rel="related" href="https://webservice.yes-co.com/3mcp/1.5/15f44340-ae6c-11e7-8f1a-0800200c9a66/media/6d65c57f-621f-4c15-8a1d-5dc967423d5d-large.jpg" type="image/jpg" mcp:mediaFormat="large"/>
    </entry>        
</feed>

到目前为止,我的代码如下所示,但是即使data变量包含上面的XML,nodeList.Count行返回0的结果:

代码语言:javascript
运行
复制
    Dim WC As New WebClient
    Dim data As String = WC.DownloadString("http://localhost/index.xml")

    Dim indexXML As New XmlDocument
    indexXML.LoadXml(data)

    Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(indexXML.NameTable)
    mgr.AddNamespace("http://www.w3.org/2005/Atom", indexXML.DocumentElement.NamespaceURI)

    Dim node As XmlNode

    Dim root As XmlNode = indexXML.DocumentElement
    Dim nodeList As XmlNodeList = root.SelectNodes("/feed/entry")

    'now loop through all elements  with "category term=collection" in index.xml
    For i As Integer = 0 To nodeList.Count - 1
        If nodeList(i).SelectSingleNode("/category/@term=collection") IsNot Nothing Then
            LogMessage(nodeList(i).SelectSingleNode("/category/link/@href").Value)
        End If
    Next i  

更新1

我想选择所有的'entry‘元素,其中它有一个带有term=collection的类别节点。该部分通过以下语句进行工作:indexXML.SelectNodes("/atom:feed/atom:entry[atom:category/@term=""collection""]", mgr)

我想从entry节点开始,然后选择条目子元素链接的href属性(以及将来entry的其他子元素)。但是,下面我尝试的示例中没有一个返回href属性的值。我怎么才能解决呢?

我现在有一个:

代码语言:javascript
运行
复制
Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(indexXML.NameTable)
mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom")

Dim root As XmlNode = indexXML.DocumentElement
Dim nodeList As XmlNodeList = indexXML.SelectNodes("/atom:feed/atom:entry[atom:category/@term=""collection""]", mgr)

'now loop through all collections in index.xml

For i As Integer = 0 To nodeList.Count - 1 '1 result found

'NONE OF CALLS BELOW RETURN THE VALUE OF HREF ATTRIBUTE
    If nodeList(i).SelectSingleNode("atom:/link/@href", mgr) IsNot Nothing Then
        LogMessage(nodeList(i).SelectSingleNode("atom:/link/@href", mgr).Value)
'error: 'atom:/link/@href' has an invalid qualified name.
    End If
Next i

由于@Pawel i,UPDATE 2能够为category节点上的term属性选择所有以project作为值的entry节点,如下所示:

代码语言:javascript
运行
复制
objectsXML.SelectNodes("/atom:feed/atom:entry[atom:category/@term=""project""]", mgr)

但是,如何向这个选择器中添加一个额外的标准,以筛选出具有节点entry的值NBvhBBvhmcp:projectScenario节点。

UPDATE 3 I向管理器添加了一个额外的命名空间:

代码语言:javascript
运行
复制
mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom")
mgr.AddNamespace("mcp", "http://webservice.yes-co.nl/3mcp/1.5/atom-extension")

但是,当我试图通过uuid选择媒体元素的href属性时,我得到了错误:Object reference not set to an instance of an object.

我的代码:

代码语言:javascript
运行
复制
objectsXML.SelectSingleNode("/atom:feed/atom:entry[atom:id=""urn:uuid:" + "6d65c57f-621f-4c15-8a1d-5dc967423d5d" + """]/mcp:link/@href", mgr).InnerText
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-04 02:41:44

文档使用http://www.w3.org/2005/Atom命名空间。您需要将此命名空间绑定到uri前缀,并在XPath中使用此前缀。如果将命名空间绑定到atom前缀,如下所示:

代码语言:javascript
运行
复制
var nsmanager = new XmlNamespaceManager(indexXML.NameTable);
nsmanager.AddNamespace("atom", "http://www.w3.org/2005/Atom");

如果您传递名称空间管理器,您将能够在XPath表达式中使用这个前缀,例如:

代码语言:javascript
运行
复制
indexXML.SelectNodes("/atom:feed/atom:entry[atom:category/@term="collection"]/atom:link/@href", nsmanager)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47103694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档