有几个相关的问题,但没有一个可以提供我需要的指导。
假设以下 XML:
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" >
<entry>
<title type="text">This is the title</title>
<content>lorem ipsum</content>
</entry>
<entry>
<title type="text">This is the second title</title>
<content>lorem ipsum 2</content>
</entry>
</feed>
如果此文档没有命名空间,我可以编写如下代码:
Public Sub ShowXML(XmlText As String)
Dim doc As New XmlDocument
doc.LoadXml(XmlText)
For Each Entry As XmlNode In doc.SelectNodes("//entry")
Console.WriteLine(Entry.SelectSingleNode("title").InnerText)
Next
End Sub
由于命名空间,这是不可能的。这是我最近的尝试:
Public Sub ShowXML(XmlText As String)
Dim doc As New XmlDocument
doc.LoadXml(XmlText)
Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("rss", "http://www.w3.org/2005/Atom")
For Each Entry As XmlNode In doc.SelectNodes("//rss:entry")
Console.WriteLine(Entry.SelectSingleNode("/rss:title").InnerText)
Next
End Sub
获取“标题”的正确 XPath 语法是什么?
为什么这是一个与类似主题的其他问题不同的问题的原因:
- 大多数其他示例都是 C#,我正在寻找 VB.NET 解决方案。
- 其他示例没有解决从先前选择的节点导航到节点的场景。
我不是在寻找剥离命名空间的解决方案;我想了解他们。谢谢你。