473,789 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do I use namespaces with XPathNavigator?

Hi,

I can't figure out for the life of me how to select nodes using a namespace
in XpathNavigator. For example:

XPathDocument doc = new XPathDocument(" file.xml");
XPathNavigator nav = doc.CreateNavig ator();
XPathNodeIterat or itr = nav.Select("/foo:bar");

Throws the exception "Namespace Manager or XsltContext needed. This query
has a prefix, variable, or user-defined function"

I notice that the Select() function of XmlDocument allows me to supply a an
XmlNamespaceMan ager object as a parameter, but neither XPathDocument or
XPathNavigator seem to have any such feature.

I'm happy to create an XmlNamespaceMan ager if someone could please explain
exactly where I should put it :)

Andy
Jan 24 '06 #1
4 17540
Andy Fish wrote:
I can't figure out for the life of me how to select nodes using a namespace
in XpathNavigator. For example:

XPathDocument doc = new XPathDocument(" file.xml");
XPathNavigator nav = doc.CreateNavig ator();
XPathNodeIterat or itr = nav.Select("/foo:bar");

Throws the exception "Namespace Manager or XsltContext needed. This query
has a prefix, variable, or user-defined function"

I notice that the Select() function of XmlDocument allows me to supply a an
XmlNamespaceMan ager object as a parameter, but neither XPathDocument or
XPathNavigator seem to have any such feature.

I'm happy to create an XmlNamespaceMan ager if someone could please explain
exactly where I should put it :)


In .NET 2.0 there is additional overload that accepts string expression
and IXmlNamespaceRe solver (XmlNamespaceMa nager implements it) - see
http://msdn2.microsoft.com/en-us/library/6k4x060d.aspx.

In .NET 1.1 you have no choice but to compile XPathExpression manually:

XPathExpression expr = nav.Compile("bo ok/@bk:ISBN");
XmlNamespaceMan ager mngr = new XmlNamespaceMan ager(new NameTable());
mngr.AddNamespa ce("bk","urn:sa mples");
expr.SetContext (mngr);
XPathNodeIterat or ni = nav.Select(expr );

See
http://msdn.microsoft.com/library/de...lecttopic2.asp

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com
Jan 24 '06 #2
>
In .NET 1.1 you have no choice but to compile XPathExpression manually:

XPathExpression expr = nav.Compile("bo ok/@bk:ISBN");
XmlNamespaceMan ager mngr = new XmlNamespaceMan ager(new NameTable());
mngr.AddNamespa ce("bk","urn:sa mples");
expr.SetContext (mngr);
XPathNodeIterat or ni = nav.Select(expr );


Thanks Oleg, it works a treat (but what a polaver though).

One small follow-up if I may. I would have thought that if I initialized the
namespace manager like this:

XmlNamespaceMan ager mngr = new XmlNamespaceMan ager(nav.NameTa ble);

then it would pick up the namespace prefixes from the document. However, it
doesn't (I still need to do the AddNamespace calls)

TIA

Andy

Jan 24 '06 #3
Andy Fish wrote:
One small follow-up if I may. I would have thought that if I initialized the
namespace manager like this:

XmlNamespaceMan ager mngr = new XmlNamespaceMan ager(nav.NameTa ble);

then it would pick up the namespace prefixes from the document. However, it
doesn't (I still need to do the AddNamespace calls)


No, first of all NameTable has nothing to do with namespaces - it's just
a collection of atomic strings (names) used to save memory and speed
up name comparisons. Second - there is no such thing as "prefixes from
the document" as in XML document namespace prefixes can be overriden and
undeclared in any order.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com
Jan 24 '06 #4


Andy Fish wrote:

One small follow-up if I may. I would have thought that if I initialized the
namespace manager like this:

XmlNamespaceMan ager mngr = new XmlNamespaceMan ager(nav.NameTa ble);

then it would pick up the namespace prefixes from the document. However, it
doesn't (I still need to do the AddNamespace calls)


The NameTable is used to speed up string comparison, there is nothing
done like "picking up namespace prefixes".
As said, with .NET 2.0 you have some way to use the XPathNavigator
itself, once moved to the proper node you want to "pick up namespace
prefixes" from, itself as the namespace manager.
<http://groups.google.c om/group/microsoft.publi c.dotnet.xml/msg/af7d7f9e2f422d7 e?hl=en&>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 24 '06 #5

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

Similar topics

1
3378
by: Craig Pearson | last post by:
Hi My function receives an XPathNavigator object. From here I need to build a DataSet to load into SQL Server (using SQLXML adaptor). Does anyone have an idea on the most efficient way to convert the XPathNavigator into a DataSet? Here is what I have at the moment: void function DoSomething( string xmlSchemeFile, System.Xml.XPath.XPathNavigator nav ) {
1
1988
by: Hollywood | last post by:
I have the following code that is attempting to read nodes from an xml file... XmlDocument xpdStatus = new XmlDocument(); xpdStatus.Load("status.xml"); XPathNavigator xpnStatus = xpdStatus.CreateNavigator(); XPathNodeIterator xpniStatus = xpnStatus.Select("descendant::Status"); while (xpniStatus.MoveNext()) {
2
1487
by: orange | last post by:
Hello, I have been trying to get data from an xml file with a namespace. I have followed the example in the MSDN, but I get no data in t he XPathNodeIterator, but the file has the data I am trying to extract. ... XPathNavigator navigator = xmlDoc.CreateNavigator(); // xpathQuery = "/ProductInformation/ProductNumber" XPathExpression expr = navigator.Compile( xpathQuery );
7
4088
by: David Thielen | last post by:
Hi; Is there a way from an XPathNavigator object to get an xpath string that will, when used in a Select(xpath) on the underlying base/root XPathNavigator return the same XPathNavigator? In other words, I initially create an XPathNavigator for my entire xml document. To get an XPathNavigator object who's root is a given node in the original xml document, there is a unique xpath that will return that node. The unique xpath could have,...
12
3965
by: David Thielen | last post by:
Hi; I have an element: <space> </space> When I call SelectSingleNode() on it, the InnerXml is a 0 length String, not a String containing 1 space. Any ideas?
3
2820
by: rdcpro | last post by:
Hi all, I've been building a nifty deserializing configuration handler that I use in conjunction with my web.config in an ASP.NET web app. This is working quite well, but I'm planning on servicing a number of different web service operations from a single application, and because it's all XML, I've been scoping everything to a specific namespace targeting a specific application (these are all InfoPath forms). It seems that the...
11
6467
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString); XPathNavigator nav = docNav.CreateNavigator(); XmlDocument doc = new XmlDocument(); doc.LoadXml("<DocumentData></DocumentData>"); XmlElement elem = doc.CreateElement(currentNodeName);
2
1949
by: Lou | last post by:
I have been working on something very simple for days now argh! In VB6 it was 4 lines of code in 5 minutes! In VB .NET how do i parse this simple xml documenet. I cant get a reference to any of the nodes. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> - <cap:alert xmlns:cap="http://www.incident.com/cap/1.0"> <cap:identifier>NOAA-NWS-ALERTS Alabama 2007-02-16T10:03:56-05:00</cap:identifier>...
2
1806
by: Brent | last post by:
I'm new to XML namespaces, and I seem to be missing something obvious in this code snippet, which is trying to retrieve the feed/entry nodes from an XPathNavigator document: XmlNamespaceManager mgr = new XmlNamespaceManager(xpNav.NameTable); mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); XPathExpression itemsExpr = xpNav.Compile("/atom:feed/ atom:entry/");
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.