entry
以下の XML をループして、基になる要素がthr属性category
の値を持つすべての要素を見つけようとしています。以下の例では 2 つあります。そして、それらの要素の属性の値を取得します。ただし、適切なセレクターが見つからないようです。collection
term
href
link
<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 の結果を返します。
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
のカテゴリ ノードを持つすべての「エントリ」要素を選択したいterm=collection
。その部分は、次のステートメントを通じて機能します。indexXML.SelectNodes("/atom:feed/atom:entry[atom:category/@term=""collection""]", mgr)
entry ノードから開始してから、 entry のサブ要素 link の href 属性を選択します (将来的には、 の他の子要素ですentry
。ただし、以下で試した例はいずれも、href
属性の値を返しません。方法私はそれを修正できますか?
私は今これを持っています:
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
更新 2
@Pawel のおかげで、次のようにノードの属性の値としてentry
持つすべてのノードを選択できました。project
term
category
objectsXML.SelectNodes("/atom:feed/atom:entry[atom:category/@term=""project""]", mgr)
ただし、このセレクターに追加の基準を追加して、 node のentry
値NBvh
ORを持つノードを除外するにはどうすればよいですか?BBvh
mcp:projectScenario
UPDATE 3 マネージャーに名前空間を追加しました:
mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom")
mgr.AddNamespace("mcp", "http://webservice.yes-co.nl/3mcp/1.5/atom-extension")
しかし、href
uuid でメディア要素の属性を選択しようとすると、エラーが発生します。Object reference not set to an instance of an object.
私のコード:
objectsXML.SelectSingleNode("/atom:feed/atom:entry[atom:id=""urn:uuid:" + "6d65c57f-621f-4c15-8a1d-5dc967423d5d" + """]/mcp:link/@href", mgr).InnerText