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

XmlDocument and Xpath

Hi ,

I have an XmlDocument instance, I want to find a node in the xml, but I
don't know it's path until runtime, for example

<aaa>
<bbb name="x"/>
<aaa attr="y">
<ccc>sometext</ccc>
</aaa>
</bbb>
</aaa>

sometime I will look for tag "aaa" (the root)
sometime I will loof for tag bbb
sometime I will look for tag the child aaa node (with attribute ="y")

for now I am using the SelectSingleNode method and specify the xPath.
But I don't know how can I found the desire node if I don't know the path.

I've tried

rootElement.SelectSingleNode("aaa[attribute::attr=\"y\"]", myNamespaceMgr);

but it doesn't work. It only work if I specify the full path i.e
aaa\bbb\aaa[attribute::attr=\"y\"], but in my case the path is dyanamic....

so my question is, given a node name and attributes (name and value), if
any, how can I find a node in the xml doc from the root element (any method
is ok, not necessary using xpath)?

thanks first,

Gnic

Mar 29 '06 #1
5 7894


Gnic wrote:

so my question is, given a node name and attributes (name and value), if
any, how can I find a node in the xml doc from the root element (any method
is ok, not necessary using xpath)?


Then use an XPath expression alike
//aaa[@attr = 'y']
that selects aaa elements at all levels without the need to specify all
ancestor names.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 29 '06 #2
thanks Martin

I tried to add the "@" in front of the attribute keywordand I got the
following error

System.Xml.XPath.XPathException

Message:
The expression passed to this method should result in a NodeSet.

Stack Track:
at System.Xml.XPath.XPathParser.ParseNodeTest(AstNode qyInput, AxisType
axisType, XPathNodeType nodeType)
at System.Xml.XPath.XPathParser.ParseStep(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseRelativeLocation Path(AstNode
qyInput)
at System.Xml.XPath.XPathParser.ParseLocationPath(Ast Node qyInput)
at System.Xml.XPath.XPathParser.ParsePathExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseUnionExpr(AstNod e qyInput)
at System.Xml.XPath.XPathParser.ParseUnaryExpr(AstNod e qyInput)
at System.Xml.XPath.XPathParser.ParseMultiplicativeEx pr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseAdditiveExpr(Ast Node qyInput)
at System.Xml.XPath.XPathParser.ParseRelationalExpr(A stNode qyInput)
at System.Xml.XPath.XPathParser.ParseEqualityExpr(Ast Node qyInput)
at System.Xml.XPath.XPathParser.ParseAndExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseOrExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseExpresion(AstNod e qyInput)
at System.Xml.XPath.XPathParser.ParsePredicate(AstNod e qyInput)
at System.Xml.XPath.XPathParser.ParseStep(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseRelativeLocation Path(AstNode
qyInput)
at System.Xml.XPath.XPathParser.ParseLocationPath(Ast Node qyInput)
at System.Xml.XPath.XPathParser.ParsePathExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseUnionExpr(AstNod e qyInput)
at System.Xml.XPath.XPathParser.ParseUnaryExpr(AstNod e qyInput)
at System.Xml.XPath.XPathParser.ParseMultiplicativeEx pr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseAdditiveExpr(Ast Node qyInput)
at System.Xml.XPath.XPathParser.ParseRelationalExpr(A stNode qyInput)
at System.Xml.XPath.XPathParser.ParseEqualityExpr(Ast Node qyInput)
at System.Xml.XPath.XPathParser.ParseAndExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseOrExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseExpresion(AstNod e qyInput)
at System.Xml.XPath.XPathParser.ParseXPathExpresion(S tring
xpathExpresion)
at System.Xml.XPath.QueryBuilder.Build(String query, Boolean allowVar,
Boolean allowKey)
at System.Xml.XPath.QueryBuilder.Build(String query, Boolean& hasPrefix)
at System.Xml.XPath.XPathNavigator.Compile(String xpath)
at System.Xml.XPath.XPathNavigator.Select(String xpath)
at System.Xml.XmlNode.SelectNodes(String xpath)
at System.Xml.XmlNode.SelectSingleNode(String xpath)

I am sure the node name and the attribute is correct (in fact I think the
specified node is not found it will return null instead of giving an
exception rite?)

Any clue?

thanks
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:ed****************@TK2MSFTNGP09.phx.gbl...


Gnic wrote:

so my question is, given a node name and attributes (name and value), if
any, how can I find a node in the xml doc from the root element (any
method is ok, not necessary using xpath)?


Then use an XPath expression alike
//aaa[@attr = 'y']
that selects aaa elements at all levels without the need to specify all
ancestor names.

--

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

Mar 29 '06 #3
Actually I can get rid of the exception now, but I still can't find the
node, it returns null when I tried to find the node.
<aaa>
<bbb name="x"/>
<aaa attr="y">
<ccc>sometext</ccc>
</aaa>
</bbb>
</aaa>

Now, I want to find the "<aaa attr="y">" node
if I tried rootElement.SelectSingleNode("aaa/bbb/aaa[@attr="y"]",
myNamespace), it works.
but when I try rootElement.SelectSingleNode ("aaa[@attr="y"]", myNamespace),
it return null.

The thing is I don't know the exact path at design time, so I have to look
for the node from the root.

any idea?

thanks

Gnic

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:ed****************@TK2MSFTNGP09.phx.gbl...


Gnic wrote:

so my question is, given a node name and attributes (name and value), if
any, how can I find a node in the xml doc from the root element (any
method is ok, not necessary using xpath)?


Then use an XPath expression alike
//aaa[@attr = 'y']
that selects aaa elements at all levels without the need to specify all
ancestor names.

--

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

Mar 29 '06 #4
Martin already answered your question. Go back and look at his sample
expression.
Specifically,

SelectSingleNode ("//aaa[@attr="y"]", myNamespace)
The double forward slash "//" indicates a deep search. There is more
overhead, but it should give you what you want.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Gnic" wrote:
Actually I can get rid of the exception now, but I still can't find the
node, it returns null when I tried to find the node.
<aaa>
<bbb name="x"/>
<aaa attr="y">
<ccc>sometext</ccc>
</aaa>
</bbb>
</aaa>

Now, I want to find the "<aaa attr="y">" node
if I tried rootElement.SelectSingleNode("aaa/bbb/aaa[@attr="y"]",
myNamespace), it works.
but when I try rootElement.SelectSingleNode ("aaa[@attr="y"]", myNamespace),
it return null.

The thing is I don't know the exact path at design time, so I have to look
for the node from the root.

any idea?

thanks

Gnic

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:ed****************@TK2MSFTNGP09.phx.gbl...


Gnic wrote:

so my question is, given a node name and attributes (name and value), if
any, how can I find a node in the xml doc from the root element (any
method is ok, not necessary using xpath)?


Then use an XPath expression alike
//aaa[@attr = 'y']
that selects aaa elements at all levels without the need to specify all
ancestor names.

--

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


Mar 29 '06 #5
oh I missed that...

it works now, thx a lot
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:B4**********************************@microsof t.com...
Martin already answered your question. Go back and look at his sample
expression.
Specifically,

SelectSingleNode ("//aaa[@attr="y"]", myNamespace)
The double forward slash "//" indicates a deep search. There is more
overhead, but it should give you what you want.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Gnic" wrote:
Actually I can get rid of the exception now, but I still can't find the
node, it returns null when I tried to find the node.
<aaa>
<bbb name="x"/>
<aaa attr="y">
<ccc>sometext</ccc>
</aaa>
</bbb>
</aaa>

Now, I want to find the "<aaa attr="y">" node
if I tried rootElement.SelectSingleNode("aaa/bbb/aaa[@attr="y"]",
myNamespace), it works.
but when I try rootElement.SelectSingleNode ("aaa[@attr="y"]",
myNamespace),
it return null.

The thing is I don't know the exact path at design time, so I have to
look
for the node from the root.

any idea?

thanks

Gnic

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:ed****************@TK2MSFTNGP09.phx.gbl...
>
>
> Gnic wrote:
>
>
>> so my question is, given a node name and attributes (name and value),
>> if
>> any, how can I find a node in the xml doc from the root element (any
>> method is ok, not necessary using xpath)?
>
> Then use an XPath expression alike
> //aaa[@attr = 'y']
> that selects aaa elements at all levels without the need to specify all
> ancestor names.
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/


Mar 30 '06 #6

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

Similar topics

5
by: John Bailo | last post by:
I wrote a webservice to output a report file. The fields of the report are formatted based on information in an in-memory XmlDocument. As each row of a SqlDataReader are looped through, a...
2
by: Jim Lewis | last post by:
After everything I have read and some of my own testing I am convinced that XPathDocument are more efficient if you are only using XML for read only and not modifying the XML. However, I have been...
1
by: Seong-Tae Jeong | last post by:
for example, xml document is below, It has a default namespace "xmlns='qwer://test'". string xmlText = "<test xmlns='http://test'><clear/><clear/></test>"; I would like to select node list...
2
by: John Barring | last post by:
Hi All, I am new to XPath stuff. I want parse XMLDocument with XPath and find out subset of information. If you look at following xml, for i.e how can i retrieve subset information such as...
6
by: David Thielen | last post by:
Hi; I am calling SelectSingleNode("/xml/s:Schema/s:ElementType/@name") where "/xml/s:Schema/s:ElementType/@name is a legit xpath statement (xml is the name of the rootnode) and that xpath...
19
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org"...
1
by: John A Grandy | last post by:
I've got an app that has hundreds of medium-sized (100s of elements) XML files on disk (not in db). Right now these are loaded via XMLDocument.Load and searched with XPATH. The performance has...
5
by: Paw Pedersen | last post by:
When you are "working" on a specific node from a XmlDocument instance, is it possible to get the full xpath to this node? Regards Paw
4
by: Daniel | last post by:
Is it possible to use regular expressions inside of an xpath statement executed by System.Xml.XmlDocument.SelectSingleNode() ? string sdoc = "<foo><bar a='1'/><bar a='2'/></foo>";...
1
by: =?Utf-8?B?RGF2aWRHQg==?= | last post by:
OK, so I've created and loaded an XMLDocument object. But how do I go about using it? Specifically, how do I: 1) move to the first node (I assume I start on it when I load the XML?) 2) move to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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
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,...

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.