0

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?

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I hadn't thought about using LINQ to XML, I haven't used that before. Now that I have looked into it, it works way better and leaves a cleaner code as well. Thanks a lot for your answer, I will mark it as the correct answer ^
0

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

1 Comment

Thank you for the answer. I have tried it, but now I'm getting System.Xml.XPath.XPathException: 'The prefix of namespace ns1 is not defined.' as an error. How can I solve that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.