472,978 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 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 6801
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.