Reputation: 53
I'm trying to select a specific <Relationships>
node with XML in VB.NET. The problem which occurs is that, with my set XPath, I get the error:
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
This is the XPath I use:
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']")
I understand that you're then supposed to add a namespace manager, which I tried doing. However, I get mixed up with all the definitions and examples I have seen and therefore I haven't got the code to work:
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")
The code above resulted in the following error at the second namespace I'm adding:
Prefix "xmlns" is reserved for use by XML.
My XML file looks like this:
<?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>
I'm not getting how you're supposed to work with these namespaces in VB.NET and incorperate them into your XPath. Is there anyone who now how to solve this and select the <Relationships>
node?
Upvotes: 0
Views: 794
Reputation: 22157
It is much better to use LINQ to XML API while dealing with XML. It is available for more than a decade.
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)
Upvotes: 1
Reputation: 22157
You need to add a namespace prefix like follows.
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")
After that, the following XPath expression will give access to the Relationships fragment.
/pkg:package/pkg:part/pkg:xmlData/ns1:Relationships
Upvotes: 0