473,473 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple question, how to do XPath search by XmlDocument with namesp

For a XML fragment like below
<ns0:BusCar_Request xmlns:ns0="http://BTSDG_SQL">
<ns0:sync>
<ns0:after>
<ns0:BusinessCards NameOnCard="NameOnCard_1"
TitleOnCard="TitleOnCard_1" Quantity="10" />
<ns0:BusinessCards NameOnCard="NameOnCard_2"
TitleOnCard="TitleOnCard_2" Quantity="10" />
<ns0:BusinessCards NameOnCard="NameOnCard_3"
TitleOnCard="TitleOnCard_3" Quantity="10" />
</ns0:after>
</ns0:sync>
</ns0:BusCar_Request>
How to use XmlDocument and XPathNavigator (preferable) to do XPath query to
get the element with 'TitleOnCard' value equals to 'TitleOnCard_3'
Thanks in advance.
Nov 12 '05 #1
6 1475
Hello!
How to use XmlDocument and XPathNavigator (preferable) to do XPath query to
get the element with 'TitleOnCard' value equals to 'TitleOnCard_3'
Thanks in advance.


You need to Compile the XPath-String into an XPathExpression and assign
an XmlNamespaceManager to its context (untested):

XPathExpression ex = doc.Compile("/a:BusCar_Request/b:something");
XmlNamespaceManager mgr = new XmlNamespaceManager( doc.NameTable );
mgr.AddNamespacePrefix( "a", "urn:foo:a" );
mgr.AddNamespacePrefix( "b", "urn:foo:b" );
ex.Context = mgr;

doc.Evaluate( ex );

--
Pascal Schmitt
Nov 12 '05 #2
For the XML segment
"<ns0:BusCar_Request xmlns:ns0="http://BTSDG_SQL">
<ns0:sync>
<ns0:after>
<ns0:BusinessCards NameOnCard="NameOnCard_1"
TitleOnCard="TitleOnCard_1" Quantity="10" />
<ns0:BusinessCards NameOnCard="NameOnCard_2"
TitleOnCard="TitleOnCard_2" Quantity="10" />
<ns0:BusinessCards NameOnCard="NameOnCard_3"
TitleOnCard="TitleOnCard_3" Quantity="10" />
</ns0:after>
</ns0:sync>
</ns0:BusCar_Request>
"

I wrote the following code to get the suitable 'ns0:BusinessCards' element,
but everytime the compile return type returns boolean, could you help please

XPathDocument doc=new XPathDocument(dataFile);
XPathNavigator navigator=doc.CreateNavigator();

XmlNamespaceManager nsmgr= new XmlNamespaceManager(navigator.NameTable);
nsmgr.AddNamespace("ns0",@"http://BTSDG_SQL");
XPathExpression
exp=navigator.Compile(@"//BusinessCards/@TitleOnCard='TitleOnCard_3'");
exp.SetContext(nsmgr);
Console.WriteLine(exp.ReturnType);

"Pascal Schmitt" wrote:
Hello!
How to use XmlDocument and XPathNavigator (preferable) to do XPath query to
get the element with 'TitleOnCard' value equals to 'TitleOnCard_3'
Thanks in advance.


You need to Compile the XPath-String into an XPathExpression and assign
an XmlNamespaceManager to its context (untested):

XPathExpression ex = doc.Compile("/a:BusCar_Request/b:something");
XmlNamespaceManager mgr = new XmlNamespaceManager( doc.NameTable );
mgr.AddNamespacePrefix( "a", "urn:foo:a" );
mgr.AddNamespacePrefix( "b", "urn:foo:b" );
ex.Context = mgr;

doc.Evaluate( ex );

--
Pascal Schmitt

Nov 12 '05 #3
Hello!
XmlNamespaceManager nsmgr= new XmlNamespaceManager(navigator.NameTable);
nsmgr.AddNamespace("ns0",@"http://BTSDG_SQL");
XPathExpression
exp=navigator.Compile(@"//BusinessCards/@TitleOnCard='TitleOnCard_3'");


You also have to use the Namespace prefix you registered in your expression:

exp=navigator.Compile(@"//ns0:BusinessCards/@TitleOnCard='TitleOnCard_3'");
--------------------------^^^
--
Pascal schmitt
Nov 12 '05 #4
I modified the code as belows:

XmlNamespaceManager nsmgr= new XmlNamespaceManager(navigator.NameTable);
nsmgr.AddNamespace("ns0",@"http://BTSDG_SQL");
XPathExpression
exp=navigator.Compile(@"//ns0:BusinessCards/@TitleOnCard='TitleOnCard_3'");
exp.SetContext(nsmgr);
Console.WriteLine(exp.ReturnType);

Still, everytime it says boolean, I am expecting a nodeset returned. Please
help, thanks a lot !!!

"Pascal Schmitt" wrote:
Hello!
XmlNamespaceManager nsmgr= new XmlNamespaceManager(navigator.NameTable);
nsmgr.AddNamespace("ns0",@"http://BTSDG_SQL");
XPathExpression
exp=navigator.Compile(@"//BusinessCards/@TitleOnCard='TitleOnCard_3'");


You also have to use the Namespace prefix you registered in your expression:

exp=navigator.Compile(@"//ns0:BusinessCards/@TitleOnCard='TitleOnCard_3'");
--------------------------^^^
--
Pascal schmitt

Nov 12 '05 #5


Tommy wrote:

exp=navigator.Compile(@"//ns0:BusinessCards/@TitleOnCard='TitleOnCard_3'"); Still, everytime it says boolean, I am expecting a nodeset returned.


I think you want an XPath expression alike
//ns0:BusinessCards[@TitleOnCard='TitleOnCard_3']
then to have a nodeset returned.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #6
Oh, my ! what a stupid mistake I made. Thanks a lot for your help, Martin.

"Martin Honnen" wrote:


Tommy wrote:

exp=navigator.Compile(@"//ns0:BusinessCards/@TitleOnCard='TitleOnCard_3'");

Still, everytime it says boolean, I am expecting a nodeset returned.


I think you want an XPath expression alike
//ns0:BusinessCards[@TitleOnCard='TitleOnCard_3']
then to have a nodeset returned.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #7

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

Similar topics

4
by: Andreas Håkansson | last post by:
I have a price of XML that looks like this <Root> <SomeNode> ..... </SomeNode> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </Signature> </Root>
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
2
by: Someone | last post by:
Using C#, ASP.NET I'm trying to implement Yahoo search API. I can sent the HTTP request and get back the results. The problem I have is that I don't know how to use XPath with the results than...
2
by: Bob | last post by:
I have sitemap like XML, of which every element has an attribute "url", e.g. <record menu_id="240" name="Countries and States" url="Countries.aspx"> <record2 page_id="54" url="CountriesEdit.aspx"...
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>
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
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>...
1
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
8
by: =?Utf-8?B?TWFyaw==?= | last post by:
Here are three things that I thought would be equivalent but are not. Just wondering why: XmlElement foo = doc.CreateElement("foo"); // load it up with a body xslt.Transform (foo, null, new...
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,...
1
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...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
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...

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.