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

help reading xml doc

I'm new to XML, so please bear with me.
I'm using XPathNavigator and XPathNodeIterator to return a set of xml nodes
from a document that looks like this:
<root>
<book title="Some Title">
<chapter>
<title>Chapter 1</title>
<starting_page>34</starting_page>
<number_of_pages>15</number_of_pages>
</chapter>
<chapter>
<title>Chapter 2</title>
<starting_page>50</starting_page>
<number_of_pages>15</number_of_pages>
</chapter>
</book>
</root>

My code:
XPathDocument xmlDoc = new XPathDocument("books.xml");
XPathNavigator xmlNav = xmlDoc.CreateNavigator();
XPathExpression xmlExpr = xmlNav.Compile("root/book[@title='Some
Title']/chapter");
XPathNodeIterator xmlItor = xmlNav.Select(xmlExpr);
xmlItor.MoveNext();

At this point I have a collection of chapter nodes in my xmlItor, but how do
I then get the data out of the title, starting_page, and number_of_pages
elements? I would prefer to be able to refer to them by element name.

Nov 12 '05 #1
1 1047
You can get another navigator out of the iterator that is positioned on each
chapter, then you can do sub-queries from there, for example:

XPathDocument xmlDoc = new XPathDocument("..\\..\\books.xml");
XPathNavigator xmlNav = xmlDoc.CreateNavigator();
XPathExpression xmlExpr = xmlNav.Compile("root/book[@title='Some
Title']/chapter");
XPathNodeIterator xmlItor = xmlNav.Select(xmlExpr);
while (xmlItor.MoveNext()) {
XPathNavigator subnav = xmlItor.Current;
XPathNavigator title = subnav.SelectSingleNode("title");
if (title != null) {
Console.WriteLine("title="+title.Value);
}
}
The subnav object is a Clone() of the original XPathNavigator. The Clone()
method is very cheap and allows you to have as many navigators on the same
tree as you need.
"David" <Da***@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
I'm new to XML, so please bear with me.
I'm using XPathNavigator and XPathNodeIterator to return a set of xml
nodes
from a document that looks like this:
<root>
<book title="Some Title">
<chapter>
<title>Chapter 1</title>
<starting_page>34</starting_page>
<number_of_pages>15</number_of_pages>
</chapter>
<chapter>
<title>Chapter 2</title>
<starting_page>50</starting_page>
<number_of_pages>15</number_of_pages>
</chapter>
</book>
</root>

My code:
XPathDocument xmlDoc = new XPathDocument("books.xml");
XPathNavigator xmlNav = xmlDoc.CreateNavigator();
XPathExpression xmlExpr = xmlNav.Compile("root/book[@title='Some
Title']/chapter");
XPathNodeIterator xmlItor = xmlNav.Select(xmlExpr);
xmlItor.MoveNext();

At this point I have a collection of chapter nodes in my xmlItor, but how
do
I then get the data out of the title, starting_page, and number_of_pages
elements? I would prefer to be able to refer to them by element name.

Nov 12 '05 #2

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

Similar topics

2
by: WAYNEL | last post by:
Hi I am trying to re-write some of the example code that Agilent gives for VB to VB.Net. In .Net I keep getting the error 'cannot change the number of dimensions of an array'. I have paste...
4
by: WAYNEL | last post by:
Hi I am trying to re-write some of the example code that Agilent gives for VB to VB.Net. In .Net I keep getting the error 'cannot change the number of dimensions of an array'. I have paste...
2
by: WAYNEL | last post by:
Hi I am trying to re-write some of the example code that Agilent gives for VB to VB.Net. In .Net I keep getting the error 'cannot change the number of dimensions of an array'. I have paste...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
2
by: ali.sobh | last post by:
Hi, i am new in c++ and i need some help with a program i need to do a universal unit converter. this calculator can only do : +, *, /, and -. it should read a string (the equation) and ask you...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.