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

Programming without XPathNavigator.SelectSingleNode (framework 1.1)

Hi, given the following simple XML fragment:

<country>
<code>ES</code>
<description>SPAIN</description>
<destination>
<code>IBZ</code>
<description>IBIZA</description>
</destination>
<destination>
<code>PMI</code>
<description>MALLORCA</description>
</destination>
<destination>
<code>MAH</code>
<description>MENORCA</description>
</destination>
</country>
<country>
<code>PT</code>
<description>PORTUGAL</description>
<destination>
<code>FAO</code>
<description>ALGARVE</description>
</destination>
</country>

I want to loop through "destinations" and obtain each destination code
and name, along with it's parent country code and name, in order to,
e.g. insert them into a database table.
Is there a more direct / intuitive way to program the selection of
child nodes values within a XPathNodeIterator loop than this?

XPathDocument doc = new XPathDocument(new XmlTextReader(sXmlDocument,
XmlNodeType.Document, null));
XPathNodeIterator it =
doc.CreateNavigator().Select("//response/country/destination");
XPathNodeIterator s;
while (it.MoveNext()) {
s = it.Current.Select("../code");
s.MoveNext();
CountryCode = s.Current.Value;
s = it.Current.Select("../description");
s.MoveNext();
CountryDescription = s.Current.Value;
s = it.Current.Select("code");
s.MoveNext();
DestinationCode = s.Current.Value;
s = it.Current.Select("description");
s.MoveNext();
DestinationDescription = s.Current.Value;
helper.ExecuteNonQuery(cs, "DestinationInsert", new object[] {
CountryCode, CountryDescription, DestinationCode,
DestinationDescription } );
}

Note: I didn't use "((IHasXmlNode)it.Current).GetNode()" to avoid DOM
objects

Thanks

Apr 12 '06 #1
1 3726


gu*************@yahoo.com.ar wrote:

Is there a more direct / intuitive way to program the selection of
child nodes values within a XPathNodeIterator loop than this?
Not really, Select is generic and powerful, there is also a specific
SelectChildren e.g here
s = it.Current.Select("code");


you could do
s = it.Current.SelectChildren("code", "");
instead, not sure whether .NET then does that more efficiently than
using the generic Select.
An alternative way using Evaluate could save you some lines e.g.

while (it.MoveNext()) {
string CountryCode = it.Current.Evaluate("string(../code)") as string;
string CountryDescription =
it.Current.Evaluate("string(../description)") as string;
string DestinationCode = it.Current.Evaluate("string(code[1])") as string;
string DestinationDescription =
it.Current.Evaluate("string(description[1])") as string;

but it is not necessarily clearer that way.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 12 '06 #2

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

Similar topics

4
by: Rune | last post by:
I have two queries that appear to be exactly the same, but one of them returns null while the other one returns a valid result! Can anyone provide an explanation to why this is so? Below is an...
4
by: David | last post by:
I'm using an XPathNodeIterator to select an element in an XML document that contains text I am going to put in a label on an aspx page. I want to be able to include HTML tags in the text read...
1
by: David Thielen | last post by:
Hi; How do I handle the following: XPathNavigator nav = ...; String str = nav.SelectSingleNode("floats/@ok").Value; // works fine - returns 1.3 nav.SelectSingleNode("floats/@ok == 1.3");...
12
by: David Thielen | last post by:
Hi; I have an element: <space> </space> When I call SelectSingleNode() on it, the InnerXml is a 0 length String, not a String containing 1 space. Any ideas?
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>
2
by: Steve | last post by:
Hi. I have ax xpath expression which returns a true or false. I don't know which method to use from the XPathNavigator object to get the result. There's not a ".SelectValue("xpath expression")...
1
by: Steven Livingstone | last post by:
Does anyone else experience this issue. If you are navigating an Xml document using XPathNavigator. You then use the MoveTo() method to go to a specific child element using the...
3
by: C | last post by:
When I try this //WebSRFTemplate/Data/Field/text() I get back the values of ALL Fields in my XML below. I only want the value (Test1) of the field name specified. Am I doing something wrong...
3
by: David Thielen | last post by:
Hi; Two questions. I have an XPathNavigator object where it's OuterXml property is "<jan>231</jan>" and I am trying to write an xpath statement that will return the "jan". XPathNavigator nav...
4
by: Bruce Sandeman | last post by:
Hi, Does anyone know how to serialize an XPathNavigator object? I have tried the following but it moans that the xpn does not have a parameterless constructor. XPathNavigator xpn =...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.