XPath and default xml namespace

  • Thread starter Thread starter Vladimir
  • Start date Start date
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
 
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.
 
Back
Top