473,372 Members | 1,116 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,372 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 7892


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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.