在 VB.NET 和 XML 中使用带有 XPath 的命名空间
Use namespaces with XPath in VB.NET with XML
我正在尝试 select VB.NET 中具有 XML 的特定 <Relationships>
节点。出现的问题是,使用我的 set XPath,我收到错误:
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
这是我使用的 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")
上面的代码在我添加的第二个命名空间中导致了以下错误:
Prefix "xmlns" is reserved for use by 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 中。现在有人知道如何解决这个问题和 select <Relationships>
节点吗?
您需要添加如下命名空间前缀。
VB.NET
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(myXmlDocument.NameTable)
namespaceManager.AddNamespace("xmlns:pkg", "http://schemas.microsoft.com/office/2006/xmlPackage")
namespaceManager.AddNamespace("xmlns:ns1", "http://schemas.openxmlformats.org/package/2006/relationships")
之后,以下 XPath 表达式将提供对 Relationships 片段的访问权限。
/pkg:package/pkg:part/pkg:xmlData/ns1:Relationships
在处理XML时使用LINQ to XML API要好得多。它可以使用十多年。
VB.NET
Dim myXmlDocument As XDocument = XDocument.Load("e:\temp\package.xml")
Dim ns0 As XNamespace = "http://schemas.microsoft.com/office/2006/xmlPackage"
Dim ns1 As XNamespace = "http://schemas.openxmlformats.org/package/2006/relationships"
Dim Relationships As XElement = myXmlDocument.Descendants(ns1 + "Relationships").FirstOrDefault()
Console.WriteLine(Relationships)
我正在尝试 select VB.NET 中具有 XML 的特定 <Relationships>
节点。出现的问题是,使用我的 set XPath,我收到错误:
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
这是我使用的 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")
上面的代码在我添加的第二个命名空间中导致了以下错误:
Prefix "xmlns" is reserved for use by 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 中。现在有人知道如何解决这个问题和 select <Relationships>
节点吗?
您需要添加如下命名空间前缀。
VB.NET
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(myXmlDocument.NameTable)
namespaceManager.AddNamespace("xmlns:pkg", "http://schemas.microsoft.com/office/2006/xmlPackage")
namespaceManager.AddNamespace("xmlns:ns1", "http://schemas.openxmlformats.org/package/2006/relationships")
之后,以下 XPath 表达式将提供对 Relationships 片段的访问权限。
/pkg:package/pkg:part/pkg:xmlData/ns1:Relationships
在处理XML时使用LINQ to XML API要好得多。它可以使用十多年。
VB.NET
Dim myXmlDocument As XDocument = XDocument.Load("e:\temp\package.xml")
Dim ns0 As XNamespace = "http://schemas.microsoft.com/office/2006/xmlPackage"
Dim ns1 As XNamespace = "http://schemas.openxmlformats.org/package/2006/relationships"
Dim Relationships As XElement = myXmlDocument.Descendants(ns1 + "Relationships").FirstOrDefault()
Console.WriteLine(Relationships)