473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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 : ke***@dotnetxpert.com
msn: ke******@magicn.com

MCSD.NET
==========================
Nov 12 '05 #1
1 6823
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.
"Seong-Tae Jeong" <ke*****@hanmail.net.korea> wrote in message
news:u1**************@TK2MSFTNGP10.phx.gbl...
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 : ke***@dotnetxpert.com
msn: ke******@magicn.com

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

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob | last post by:
Below is some code that use to work. Now it is not and I have yet to figure out why. I want to select the nodes to a nodelist and remove them from the dom. The only thing I can figure is it broke...
2
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file -...
1
by: Don | last post by:
Hi: I have the following web service object serialized into my XML DOM. With XPath, I am able to select '//document/Info/ID' from the XML. Adding the namespace is where I get a problem(see...
6
by: David Thielen | last post by:
Hi; I am calling SelectSingleNode("/xml/s:Schema/s:ElementType/@name") where "/xml/s:Schema/s:ElementType/@name is a legit xpath statement (xml is the name of the rootnode) and that xpath...
19
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org"...
6
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
3
by: Yair Cohen | last post by:
hello 1 i use an xml documet with xml schema but when i try to use XPath (selectnodes) it retrieves empty list. when i remove the schema (the 'xmlns="http..."') i get the data. how can i handle...
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
6
by: Derek Hart | last post by:
I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I run xpath statements against the xml file to grab data from it, so I use, as an example, //Vehicles/Vehicles/@make to get...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.