Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to ignore namespaces in c# when using xPath?

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>
like image 730
Exitos Avatar asked Nov 30 '10 12:11

Exitos


People also ask

Can I make XmlSerializer ignore the namespace on deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.

How do I remove namespace prefix?

Once a namespace prefix is created, it cannot be changed or deleted. The workaround is to move all your code to a new Developer Organization, where you can setup the desired Namespace Prefix.

How do I get rid of namespaces?

Remove namespaces using the Namespace Editor Determine the namespace for a node by selecting any object/node that uses the namespace. In the Namespace Editor, select the namespace you want to remove. Click Delete.


1 Answers

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.

like image 51
Martin Honnen Avatar answered Oct 09 '22 12:10

Martin Honnen