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

Using XPath with namespaces

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 );
expr = navigator.Compile( xpathQuery );

XmlNamespaceManager nsmgr = new XmlNamespaceManager( navigator.NameTable
);
nsmgr.AddNamespace( "myNS",
"http://www.orange.com/schemas/imaging/cyanblue/dev/1.0" );

expr.SetContext(nsmgr);
XPathNodeIterator iter = navigator.Select( xpathQuery );

while ( iter.MoveNext() )
{
XPathNavigator innerNav = iter.Current.Clone();
innerNav.MoveToFirstChild();
retVal = innerNav.Value;
}
..

As I stated earlier, "iter" has a count of 0 - has no nodes in it. Is
there somethign I ought to be doingthat I am not doing? What could be
the problem?

Thanks for your help.

orange

*** Sent via Developersdex http://www.developersdex.com ***
Nov 12 '05 #1
2 1466


orange wrote:

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 );
expr = navigator.Compile( xpathQuery );

XmlNamespaceManager nsmgr = new XmlNamespaceManager( navigator.NameTable
);
nsmgr.AddNamespace( "myNS",
"http://www.orange.com/schemas/imaging/cyanblue/dev/1.0" );

expr.SetContext(nsmgr);
XPathNodeIterator iter = navigator.Select( xpathQuery );


Make sure your XPath expression makes use of the prefix(es) bound to
namespace URI e.g.
navigator.Select("myNS:element1/myNS:element2")

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
hi,
there are a couple of things missing.
1.the xpathQuery declaration statement needs to be uncommented.
2.the xpathQuery needs to have the prefix , if the input xml document has
the prefix for example the xpath needs to be
"myNS:ProductInformation/myNS:ProductNumber" if ProductInformation is the
root element and the myNS prefix is present in the xml dcocument.

3. the statement XPathNodeIterator iter = navigator.Select( xpathQuery );
needs to have the namespace resolver in it like
XPathNodeIterator iter = navigator.Select( xpathQuery, nsmgr );
hope this helps,
swapna


"orange" <or****@devdex.com> wrote in message
news:uT**************@TK2MSFTNGP14.phx.gbl...
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 );
expr = navigator.Compile( xpathQuery );

XmlNamespaceManager nsmgr = new XmlNamespaceManager( navigator.NameTable
);
nsmgr.AddNamespace( "myNS",
"http://www.orange.com/schemas/imaging/cyanblue/dev/1.0" );

expr.SetContext(nsmgr);
XPathNodeIterator iter = navigator.Select( xpathQuery );

while ( iter.MoveNext() )
{
XPathNavigator innerNav = iter.Current.Clone();
innerNav.MoveToFirstChild();
retVal = innerNav.Value;
}
.

As I stated earlier, "iter" has a count of 0 - has no nodes in it. Is
there somethign I ought to be doingthat I am not doing? What could be
the problem?

Thanks for your help.

orange

*** Sent via Developersdex http://www.developersdex.com ***

Nov 12 '05 #3

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

Similar topics

2
by: Piet | last post by:
Hello, Via Xpath, I want to access nodes which have a namespace prefix. THe document at hand is an Xsl-FO document. I tried the following: from xml.dom import minidom from xml.xpath import...
3
by: Colin Green | last post by:
I have come across what seems like a failing in the .Net XML classes. Many people have posted requesting how to write an XPath query with namespace prefixes that works. The solution shown in all...
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: 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>...
14
by: Mat| | last post by:
Hello :-) I am learning XPath, and I am trying to get child nodes of a node whose names do *not* match a given string, e.g : <dummy> <example> <title>Example 1</title> <body>this is an...
14
by: Mikhail Teterin | last post by:
Hello! What's would be the syntax for a query, which would allow me to get only the elements with non-empty text-nodes? For example, from: <a><b></b></a> <c/> <d><e>meow</e></d>
2
by: A. W. Dunstan | last post by:
I'm trying to figure out how XPath expressions work, and how I can use them to extract data into a particular format. I can extract the data I want using an XPath expression, but not with an XSLT...
1
by: reyesvsn | last post by:
Hello everybody, I have a question concerning XPath expressions and namespaces. Consider this XML: <?xml version="1.0" encoding="UTF-8"?> <newsMessage ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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:
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
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
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...

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.