【发布时间】:2020-01-22 20:45:36
【问题描述】:
我正在尝试在 VB.NET 中使用 XML 选择特定的 <Relationships> 节点。发生的问题是,使用我设置的 XPath,我收到错误:
需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。
这是我使用的 XPath:
Dim parentNode As XmlNode = myXmlDocument.SelectSingleNode("/pkg:package/pkg:part[@pkg:name='/_rels/.rels']/pkg:xmlData/Relationships[@xmlns='http://schemas.openxmlformats.org/package/2006/relationships']")
我知道您应该添加一个命名空间管理器,我尝试这样做。但是,我混淆了我看到的所有定义和示例,因此我没有让代码工作:
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(myXmlDocument.NameTable)
namespaceManager.AddNamespace("xmlns:pkg", "http://schemas.microsoft.com/office/2006/xmlPackage")
namespaceManager.AddNamespace("xmlns", "http://schemas.openxmlformats.org/package/2006/relationships")
上面的代码导致我添加的第二个命名空间出现以下错误:
前缀“xmlns”保留给 XML 使用。
我的 XML 文件如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="PowerPoint.Show"?>
<pkg:package
xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
<pkg:xmlData>
<Relationships
xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
</pkg:package>
我不明白你应该如何在 VB.NET 中使用这些命名空间将它们合并到你的 XPath 中。有没有人现在如何解决这个问题并选择<Relationships> 节点?
【问题讨论】:
标签: xml vb.net xml-namespaces