Groups | Blog | Home
all groups > dotnet xml > april 2004 >

dotnet xml : Navigating DOM with XPath in .NET



scorpion74
4/7/2004 9:21:10 AM
I have following xml file and I want to read <EquipmentClass> tag and it's Text.This xml file implements B2MML schema and hence xml file structure is not the usual one.Does anybody know how to ahieve the result?I tried using "SelectNodes" method but it doesn't work

Derek Harmon
4/7/2004 7:56:32 PM
[quoted text, click to view]

What's unusual about it?

[quoted text, click to view]

How did you specify the namespace URI, http://www.wbf.org/xml/b2mml-v02,
in the XPath expression when you called SelectNodes( )?

This is the likely culprit, at least it's the most frequently asked about.

XmlNamespaceManager nsMan = new XmlNamespaceManager( xmlDoc.NameTable);
nsMan.AddNamespace( "ns1", "http://www.wbf.org/xml/b2mml-v02");
XmlNodeList results = xmlDoc.SelectNodes( "//ns1:EquipmentClass/ns1:ID", nsMan);
if ( null != results )
foreach ( XmlNode n in results )
Console.WriteLine( n.InnerText);

Since you can't specify a prefix for the default namespace in the XPath
expression, it needs a prefix associated with that default namespace URI
just for purposes of writing the expression. In order to evaluate the ex-
pression, it needs a reference to a Namespace Manager supplied to
the SelectNodes( ), which has an overload accepting XmlNamespace-
Manager.


Derek Harmon

AddThis Social Bookmark Button