473,772 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
XPathNodeIterat or, but the file has the data I am trying to extract.

...
XPathNavigator navigator = xmlDoc.CreateNa vigator();
// xpathQuery = "/ProductInformat ion/ProductNumber"
XPathExpression expr = navigator.Compi le( xpathQuery );
expr = navigator.Compi le( xpathQuery );

XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager( navigator.NameT able
);
nsmgr.AddNamesp ace( "myNS",
"http://www.orange.com/schemas/imaging/cyanblue/dev/1.0" );

expr.SetContext (nsmgr);
XPathNodeIterat or iter = navigator.Selec t( xpathQuery );

while ( iter.MoveNext() )
{
XPathNavigator innerNav = iter.Current.Cl one();
innerNav.MoveTo FirstChild();
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 1486


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
XPathNodeIterat or, but the file has the data I am trying to extract.

..
XPathNavigator navigator = xmlDoc.CreateNa vigator();
// xpathQuery = "/ProductInformat ion/ProductNumber"
XPathExpression expr = navigator.Compi le( xpathQuery );
expr = navigator.Compi le( xpathQuery );

XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager( navigator.NameT able
);
nsmgr.AddNamesp ace( "myNS",
"http://www.orange.com/schemas/imaging/cyanblue/dev/1.0" );

expr.SetContext (nsmgr);
XPathNodeIterat or iter = navigator.Selec t( xpathQuery );


Make sure your XPath expression makes use of the prefix(es) bound to
namespace URI e.g.
navigator.Selec t("myNS:element 1/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:ProductIn formation/myNS:ProductNum ber" if ProductInformat ion is the
root element and the myNS prefix is present in the xml dcocument.

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


"orange" <or****@devdex. com> wrote in message
news:uT******** ******@TK2MSFTN GP14.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
XPathNodeIterat or, but the file has the data I am trying to extract.

..
XPathNavigator navigator = xmlDoc.CreateNa vigator();
// xpathQuery = "/ProductInformat ion/ProductNumber"
XPathExpression expr = navigator.Compi le( xpathQuery );
expr = navigator.Compi le( xpathQuery );

XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager( navigator.NameT able
);
nsmgr.AddNamesp ace( "myNS",
"http://www.orange.com/schemas/imaging/cyanblue/dev/1.0" );

expr.SetContext (nsmgr);
XPathNodeIterat or iter = navigator.Selec t( xpathQuery );

while ( iter.MoveNext() )
{
XPathNavigator innerNav = iter.Current.Cl one();
innerNav.MoveTo FirstChild();
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
2050
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 Evaluate from xml import sax parser = sax.make_parser() parser.setFeature(sax.handler.feature_namespaces,1) parser.setFeature(sax.handler.feature_namespace_prefixes,1) doc = minidom.parse("fo-file.xml",parser)
3
2400
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 the posts I have seen is of the form: --------------------------------------------- XmlDocument doc = new XmlDocument(); doc.Load(@"c:\foobar.xml"); XmlNamepaceManager nmMgr = newNamespaceManager(doc); nmMgr.AddNamespace("abc", "www.my.uri");
1
1612
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 XML below). -------------------------- <document> <Info xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ID>416</LMID>
6
15511
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
2875
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 this ? using xmlDocument.documentElement.selectNodes("PROP") something like this:
3
6808
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> </Root> I would like to retreive "\Root\Customer\Name" out of it. Something like:
14
2008
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 example</body>
14
17184
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
3044
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 stylesheet. Here's the Java/XPath I'm using: import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.SAXException;
1
3110
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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rtr="http://www.reuters.com/ns/2003/08/content" xmlns="http://iptc.org/std/nar/2006-10-01/"> <header>
0
9621
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
10264
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9914
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...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
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...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
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
3
2851
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.