2

I have the following XML document, and I've been using XPath to navigate this and other documents without issues. But one of the elements has an attribute with a namespace that I can't seem to navigate to with XPath.

<Pages xmlns='http://schemas.microsoft.com/office/visio/2012/main' xmlns:r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' xml:space='preserve'>
    <Page ID='33' NameU='Background-1' Name='Background-1' Background='1' ViewScale='0.75' ViewCenterX='4' ViewCenterY='2'>
        <PageSheet LineStyle='0' FillStyle='0' TextStyle='0' UniqueID='{0}'>
            <Cell N='PageWidth' V='11' U='MM'/>
            <Section N='Layer'>
                <Row IX='0'>
                    <Cell N='Name' V='Background'/>
                </Row>
                <Row IX='1'>
                    <Cell N='Name' V='Watermark'/>
                </Row>
            </Section>
        </PageSheet>
        <Rel r:id='rId1'/>
    </Page>
</Pages>

I've used XPath to select elements and attributes but I can't get it to work with an attribute that has a namespace. I have my NameSpaceManager set up like this

XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
nsm.AddNamespace("xVis", "http://schemas.microsoft.com/office/visio/2012/main");
nsm.AddNamespace("xRel", "http://schemas.openxmlformats.org/package/2006/relationships");

And I've no problems getting attributes like ID, NameU and so on using XPath like

XPathSelectElement(".//xVis:Page[@NameU='something']", nsm);

But I can't get it to work when the attribute has a namespace like the r:id attribute

XPathSelectElement(".//xVis:Page/xVis:Rel[@xRel:id='rid1']", nsm);

This XPath doesn't return anything, and neither does it if I skip the NS declaration.

How do I access this attribute?

2 Answers 2

2

There are two problems. The namespace doesn't match and the attribute value doesn't match.

The namespace of the attribute and what you are using in your XPath don't match.

The XML document has the r attribute namespace declared as:

http://schemas.openxmlformats.org/officeDocument/2006/relationships

You declared the xRel namespace as:

http://schemas.openxmlformats.org/package/2006/relationships

And the attribute value in the XML document is: rId1 but you are matching rid1

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the rId1 was a typo when i transferred it to SO. It was just the NS that was wrong. It works now. Thanks!
1

I just realised the namespaces weren't the same. And both of them are used in different documents. This solved my problem.

http://schemas.openxmlformats.org/package/2006/relationships

http://schemas.openxmlformats.org/officeDocument/2006/relationships"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.