Error in Xpath

M

miztaken

Hi there,
I need a help on making Xpaht for following xml file

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<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="word/
document.xml" />
</Relationships>

I need to develop a xpath based on Type attribute so that i can fetch
value in Target attribute.
I have loaded the document in XmlDocument and how want to select the
node using SelectNodes() method.

I have used following XPath query but its not working?

XmlElement element = doc.DocumentElement;
XmlNodeList nodes = element.SelectNodes("/Relationships/
Relationship[@Type=\"http://schemas.openxmlformats.org/officeDocument/
2006/relationships/officeDocument\"]");

Please help me

thankYou
miztaken
 
M

Marc Gravell

You have xml with namespaces, which makes life much trickier. To
illustrate, I've used a namespace-manager to give the alias "r" to that
pesky uri (below).

Marc

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlElement element = doc.DocumentElement;
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("r",element.NamespaceURI);
XmlNodeList nodes =
element.SelectNodes("/r:Relationships/r:Relationship[@Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\"]",
mgr);
foreach (XmlElement el in nodes)
{
Console.WriteLine(el.Name);
}
 
M

miztaken

Hi there,
Thank you for your snippet.
It worked. :)

Can you suggest me some good book regarding XML and C#

Take Care
miztaken
 
M

Marc Gravell

Sorry, but I haven't looked... I tacked xml "back in the day" before
moving to C#, so I've never had to look at them both at the same time.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top