473,396 Members | 1,748 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,396 software developers and data experts.

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.CreateNavigator();
XPathNodeIterator 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
XmlNamespaceManager object as a parameter, but neither XPathDocument or
XPathNavigator seem to have any such feature.

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

Andy
Jan 24 '06 #1
4 17516
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.CreateNavigator();
XPathNodeIterator 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
XmlNamespaceManager object as a parameter, but neither XPathDocument or
XPathNavigator seem to have any such feature.

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


In .NET 2.0 there is additional overload that accepts string expression
and IXmlNamespaceResolver (XmlNamespaceManager 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("book/@bk:ISBN");
XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
mngr.AddNamespace("bk","urn:samples");
expr.SetContext(mngr);
XPathNodeIterator 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("book/@bk:ISBN");
XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
mngr.AddNamespace("bk","urn:samples");
expr.SetContext(mngr);
XPathNodeIterator 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:

XmlNamespaceManager mngr = new XmlNamespaceManager(nav.NameTable);

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:

XmlNamespaceManager mngr = new XmlNamespaceManager(nav.NameTable);

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:

XmlNamespaceManager mngr = new XmlNamespaceManager(nav.NameTable);

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.com/group/microsoft.public.dotnet.xml/msg/af7d7f9e2f422d7e?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
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...
1
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 =...
2
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...
7
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...
12
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
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...
11
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);...
2
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...
2
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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.