j4m4l
j4m4l

Reputation: 328

Unable to read xml with XPath

I need to parse content.xml file in XMind package.
File looks like this

<xmap-content xmlns="urn:xmind:xmap:xmlns:content:2.0" xmlns:fo="http://www.w3.org/1999/XSL   /Format" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" version="2.0">

   <sheet id="sdfasdfsdf">
      <title> Sheet1 </title>
   </sheet>

   <sheet id="fgasdfgasr">
       <title> Sheet2 </title>
   </sheet>
<xmap-content>

I need to get list of title of all sheets.

I am using XPathExpression expr = navigator.Compile("//sheet");
but i guess its not working
what to do. any suggestion or code will do.
thnx in advance.

Upvotes: 0

Views: 815

Answers (2)

Damith
Damith

Reputation: 63065

try

XPathExpression expr =  navigator.Compile("xmap-content/sheet");

EDIT:

string st = @"<xmap-content xmlns=""urn:xmind:xmap:xmlns:content:2.0"" xmlns:fo=""http://www.w3.org/1999/XSL   /Format"" xmlns:svg=""http://www.w3.org/2000/svg"" xmlns:xhtml=""http://www.w3.org/1999/xhtml"" xmlns:xlink=""http://www.w3.org/1999/xlink"" version=""2.0"">

            <sheet id=""sdfasdfsdf"">
                <title> Sheet1 </title>
            </sheet>

            <sheet id=""fgasdfgasr"">
                <title> Sheet2 </title>
            </sheet>
        </xmap-content>";

XmlDocument document = new XmlDocument();
document.LoadXml(st);

XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable); 

manager.AddNamespace("xm", "urn:xmind:xmap:xmlns:content:2.0"); 

var titles = document.SelectNodes("//xm:sheet/xm:title", manager);

Upvotes: 0

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Your XML has defined default namespace: urn:xmind:xmap:xmlns:content:2.0. So you need to pass it to your XML engine with prefix, then use expression:

//ns:sheet

or another way:

//*[local-name() = 'sheet']

Upvotes: 2

Related Questions