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

dotnet xml : Using .NET XmlDocument Class, How to use a xpath expression on xml document which has a default namespace


Dare Obasanjo [MSFT]
4/19/2004 7:31:26 PM
You have a default namespace declared in your XML document. You need to add
the namespace to the XmlNamespaceManager you use with the XmlDocument and
prefix the namespace

xsn.AddNamespace( "t", "http://test" );

string xPath = "//t:clear";


Or remove the namespace declaration from your doc and un-prefixed element
names will work in your Xpath expressions.


--
This posting is provided "AS IS" with no warranties, and confers no rights.
[quoted text, click to view]

Seong-Tae Jeong
4/20/2004 10:33:34 AM
for example, xml document is below, It has a default namespace
"xmlns='qwer://test'".

string xmlText = "<test
xmlns='http://test'><clear/><clear/></test>";

I would like to select node list "clear" with XPath Expression.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml( xmlText );

XmlNamespaceManager xsn = new XmlNamespaceManager(
xmlDoc.NameTable );
xsn.AddNamespace( "", "http://test" );

string xPath = "//clear";
XmlNodeList nodes = xmlDoc.SelectNodes( xPath, xsn );


But, the result of "nodes.Count" is always "zero".

How can I select node list of "clear" ?

--
======= .NETXpert ==========
url : http://www.dotnetxpert.com
eml : kevin@dotnetxpert.com
msn: kevin025@magicn.com

MCSD.NET
==========================

AddThis Social Bookmark Button