Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPAth Searches in .NET 1

Status
Not open for further replies.

tomandsandy

Programmer
Sep 19, 2001
24
US
I am trying to parse a DOM object in C#. I know the source of the failure but I do not know the reason for its failure.

I am searching for a tag called <mimeType>jpeg</mimeType> using:

XmlNode bookNode = xmldoc.SelectSingleNode("//mimeType");

//Determine whether a matching node is located.
if (!(bookNode == null))
{
System.Diagnostics.Debug.WriteLine("Located base64 Image Data");
System.Diagnostics.Debug.WriteLine bookNode.FirstChild.Value);
}

It works if
I manually edit the root element. The root element is returned from a web service and conatins an xmlns attribute:
<EDXLDistribution xmlns="urn:eek:asis:names:tc:emergency:EDXL:DE:1.0">

If I perform the Select SingleNode on the DOM with the xmlns it fails every time. if i remove the xmlns="urn:eek:asis:names:tc:emergency:EDXL:DE:1.0" attribute from the root element, the SelectSingleNode succeeds every time.

Why does this matter?

Thank You

Tom
 
You need to use the XML namespace manager because XPath does not have the concept of default namespace.

I was able to Google this which shows you how to provide a set of namespaces to SelectSingleNode.

Tom Morrison
 
You've to declare it, something like this.
[tt] xmldoc.setProperty("SelectionNamespaces", "xmlns:x="urn:eek:asis:names:tc:emergency:EDXL:DE:1.0");[/tt]
With prefix x, you can search with the xpath.
[tt] XmlNode bookNode = xmldoc.SelectSingleNode("//x:mimeType");[/tt]
 
Amendment
Should escape and match quotes! The corresponding line should read like this.
[tt] xmldoc.setProperty("SelectionNamespaces", "xmlns:x=[red]\[/red]"urn:eek:asis:names:tc:emergency:EDXL:DE:1.0[red]\"[/red]");[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top