Question

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>
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top