473,403 Members | 2,354 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,403 software developers and data experts.

XML namespace specified and can't find element

I have a very simple XML document.
<SmtpGatewayRules xmlns="http://www.foo.com/schema.xsd">
<rules>
<rule>
</rule>
</rules>
</SmtpGatewayRules>

When no xmlns is specified I can find rule element
with calling: SelectSingleNode("//rule");
But when namespace is specified as in the enclosed example
no node is found. Actually no element can be found in the
entire document when explicit name of it is specified.

However, when calling
SelectNodes("//*").Count
it returns 3 (correct).
What should I do to to find the element with XPath ?
should I prefix the XPath statement with namespace name
(but how, since this is a default namespace)
Or maybe should I use XmlNamespaceManager class when
calling the SelectSingleNode(XPath,namespaceManager)... ?

I tried the second option but this did not help.
I used the following code:

XmlNamespaceManager nm=new XmlNamespaceManager
(xmlDoc.NameTable);
nm.AddNamespace("","http://www.foo.com/schema.xsd");
XmlNode node = doc.SelectSingleNode("//rule",nm);

//node is still null :-(

Thanks for help
Michal Januszczyk

Nov 12 '05 #1
2 3964
Michal Januszczyk wrote:
What should I do to to find the element with XPath ?
should I prefix the XPath statement with namespace name Yes, you have to.
(but how, since this is a default namespace) Remember main rule of namespaces - prefix doesn't matter. Just use any
you like. What only matters is namespace URI.
Or maybe should I use XmlNamespaceManager class when
calling the SelectSingleNode(XPath,namespaceManager)... ?

I tried the second option but this did not help.
I used the following code:

XmlNamespaceManager nm=new XmlNamespaceManager
(xmlDoc.NameTable);
nm.AddNamespace("","http://www.foo.com/schema.xsd");


It's a faq. See also SelectNodes method description at MSDN.

XmlNamespaceManager nm=new XmlNamespaceManager(xmlDoc.NameTable);
nm.AddNamespace("foo","http://www.foo.com/schema.xsd");
XmlNode node = doc.SelectSingleNode("//foo:rule",nm);
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #2
>-----Original Message-----
XmlNamespaceManager nm=new XmlNamespaceManager
(xmlDoc.NameTable);
nm.AddNamespace("foo","http://www.foo.com/schema.xsd");
XmlNode node = doc.SelectSingleNode("//foo:rule",nm);


Great thanks!

Michal
Nov 12 '05 #3

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

Similar topics

3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
0
by: S ML | last post by:
Hello, I use the Xerces XS* classes to parse and get the definition of an XML schema. I don't seem to find a way to get the namespace prefixes using this framework and I read somewhere that the...
2
by: Stanimir Stamenkov | last post by:
I'm trying to find out if it is permissible to include a schema document with absent target namespace to a schema with specified target namespace, and if it is, what are the rules to resolve the...
2
by: Daniel Lidström | last post by:
Hi, the class looks like this: public __gc class LandXML { public: ... };
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
5
by: David Thielen | last post by:
Hi; I set up my xml as follows: XmlDocument xml = new XmlDocument(); xml.Load(File.Open("data.xml", FileMode.Open, FileAccess.Read)); XmlNamespaceManager context = new...
4
by: Karine Bosch | last post by:
Hi, My xml document uses different namespaces: <x:root xmlns:x="x:ns:meta/"> <element1 xmlns:adam="http://ns.alfaprint.be/"> </root> This gives problems when using an XPath query. I...
12
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST>
9
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.