XPath and default xml namespace

V

Vladimir

Hello,

I have XML files with defined namespaces + defined default namespace:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="default.schema" xmlns:s="s.schema">

However when I want to run XPath query, I must give name to default
namespace:

XmlDocument doc = new XmlDocument();
doc.Load(sFileName);

XmlNamespaceManager xmlns = new XmlNamespaceManager(doc.NameTable);
xmlns.AddNamespace(string.Empty, "default.schema");
xmlns.AddNamespace("i", "default.schema");
xmlns.AddNamespace("s", "s.schema");

XmlNode node =
doc.SelectSingleNode("/i:Roott/i:parameters/i:parameter[@Name=\"ServiceXML\"]",
xmlns);


So, are there ways to make
doc.SelectSingleNode("/Roott/Parameters/Parameter[@Name=\"ServiceXML\"]",
xmlns); return values and not use artifically added "i" namespace?

Thanks,
Vladimir
 
A

Alberto Poblacion

Vladimir said:
So, are there ways to make
doc.SelectSingleNode("/Roott/Parameters/Parameter[@Name=\"ServiceXML\"]",
xmlns); return values and not use artifically added "i" namespace?

The only way I've found is to load the XML into a string, remove the
default namespace declaration, and then load the XmlDocument from the
modified string. It's not elegant, but it will let you write the XPath
queries without inserting a prefix for the namespace.
 

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