namespacemanager and selectsinglenode

M

Martin

Hi,

I am using xmldocument.selectsinglenode to query an xml document.
This works perfectly when the document to be queried has no namespace,
however as some as i put a namespace in then null is returned from
selectsinglenode.

I think I am not creating the namespace manager correctly.

I have created a very small sample piece of code (see below) that
demonstates the problem.

If anybody could point out my mistake based on the code below then I would
be extremly greatful.

many thanks in advance.

cheers

martin.



//paste the code below into the main function of a console app.

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(@"<?xml version=""1.0""?><Addresses
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns=""http://mydomain.com/TimeSeriesSchema.xsd""><Address>38 some
road</Address></Addresses>");
//comment the line above and uncomment the line below and everything
works fine.
//xmldoc.LoadXml(@"<?xml version=""1.0""?><Addresses><Address>38 some
road</Address></Addresses>");
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(xmldoc.NameTable);
namespaceManager.AddNamespace("","http://www.w3.org/2001/XMLSchema");

Console.WriteLine(xmldoc.SelectSingleNode("Addresses/Address").OuterXml.ToSt
ring() , namespaceManager);
//xmldoc.Save(Console.Out);
return;
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top