472,119 Members | 1,130 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 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 7761


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John Barring | last post: by
6 posts views Thread by David Thielen | last post: by
1 post views Thread by John A Grandy | last post: by
1 post views Thread by =?Utf-8?B?RGF2aWRHQg==?= | last post: by

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.