23

I could be given either of the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<dc:video xmlns:dc="http://purl.org/dc/elements/1.1/">
  <dc:title>
    A vid with Pete
  </dc:title>
  <dc:description>
  Petes vid
  </dc:description>
  <dc:contributor>
    Pete
  </dc:contributor>
  <dc:subject>
    Cat 2
  </dc:subject>
</dc:video>

Or:

<?xml version="1.0" encoding="UTF-8"?>
<video>
  <title>
    A vid with Pete
  <title>
  <description>
  Petes vid
  <description>
  <contributor>
    Pete
  <contributor>
  <subject>
    Cat 2
  <subject>
</video>

Im trying to access an element:

string title = xmlDocFromOneLan.SelectSingleNode(@"/video/title").InnerXml;

But with xml document 1 it doesnt work due to the namespace.

Is there a way in c# to ignore the namespace using xpath? I simply want to select the node I really dont care about the namespace. (the namespace could be DC DN or DCN etc).

"/video"

would read:

<video></video>
or
<dc:video></video>
or
<dcn:video></video>
Exitos
  • 29,230
  • 38
  • 123
  • 178

1 Answers1

48

With XPath 1.0 all you can do is /*[local-name() = 'video']/*[local-name() = 'title']. With XPath 2.0 you can use a wildcard /*:video/*:title.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks Martin, do you know where I can get documentation relating to that? – Exitos Nov 30 '10 at 12:27
  • 3
    Pete, XPath 1.0 and 2.0 are W3C specifications you can find online: http://www.w3.org/TR/xpath/, http://www.w3.org/TR/xpath20/. Microsoft only supports XPath 1.0, they do have documentation on that on MSDN: http://msdn.microsoft.com/en-us/library/ms256115.aspx. XPath 2.0 is implemented by third parties like XQSharp http://www.xqsharp.com/ or Saxon 9 http://saxon.sourceforge.net/. Saxon also has a good documentation, the *:foo syntax is documented at http://www.saxonica.com/documentation/expressions/axissteps.xml. – Martin Honnen Nov 30 '10 at 12:43