XmlDocument and Xpath

G

Gnic

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
 
M

Martin Honnen

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.
 
G

Gnic

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.ParseRelativeLocationPath(AstNode
qyInput)
at System.Xml.XPath.XPathParser.ParseLocationPath(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParsePathExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseUnionExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseUnaryExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseMultiplicativeExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseAdditiveExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseRelationalExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseEqualityExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseAndExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseOrExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseExpresion(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParsePredicate(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseStep(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseRelativeLocationPath(AstNode
qyInput)
at System.Xml.XPath.XPathParser.ParseLocationPath(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParsePathExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseUnionExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseUnaryExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseMultiplicativeExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseAdditiveExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseRelationalExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseEqualityExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseAndExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseOrExpr(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseExpresion(AstNode qyInput)
at System.Xml.XPath.XPathParser.ParseXPathExpresion(String
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 said:
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.
 
G

Gnic

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 said:
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.
 
G

Guest

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 said:
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 said:
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.
 
G

Gnic

oh I missed that...

it works now, thx a lot


Peter Bromberg said:
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 said:
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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top