Solved

500pts: Help with crawling an xml file

Posted on 2006-11-03
4
262 Views
Last Modified: 2010-04-16
Hi there,

I wonder if anyone can help me.... I have some xml data that is in a string..... I need to be able to crawl it, I add each "book" to a custom class i have. Problem being is that after each book ... I add this class to a Hashtable and add a new Class of book and start populating it.

Here is the xml file, contains 2 books but in reality there are arond 30. Below the XML file i am placing the code i currently use....it works at the moment but I don't know when I finish the v item which of course will signal me to finish the class and add to the hashtable and then when i hit a new book which will signal me to add a new class..

I hope someone can help, i am really stuck here.. Thanks in advance .   Ian

<Books>
<book name='ABC' section='SCIENCE' >
<f ii='12'>
<v item='1.9106' />
</f>
</book>
<book name='FGH' section='DRAMA' >
<f ii='11'>
<v item='1.9051' />
</f>
</book>
</Books>

Here is the code... but i don't know when i am hitting a new book etc.. or finishing a book so i don't have a signal to create a new class. finish a class and add to hashtable...

// lets get the fields and values using Xpath
                              // that were returned to use
                              StringReader myXML = new StringReader(xmlReturned); // you can do this if u have a root tag

                              XPathDocument document = new XPathDocument(myXML);
                              XPathNavigator navigator = document.CreateNavigator();

                              // Expressions for both id, numeric value, string value and nemo
                              XPathExpression expr1 = navigator.Compile("/Books/book/f/@ii");
                              XPathExpression expr2 = navigator.Compile("/Books/book/f/v/@item");
                              XPathExpression expr3 = navigator.Compile("/Books/book/@name");
                              XPathExpression expr4 = navigator.Compile("/Books/book/@section");
                              XPathExpression expr5 = navigator.Compile("/Books/book/@isin");

                              // Create iterators
                              XPathNodeIterator nodes1 = navigator.Select(expr1);
                              XPathNodeIterator nodes2 = navigator.Select(expr2);
                              XPathNodeIterator nodes3 = navigator.Select(expr3);
                              XPathNodeIterator nodes4 = navigator.Select(expr4);
                              XPathNodeIterator nodes5 = navigator.Select(expr5);


                              Book book = new Book(); /// How would i know when to create a new Book class

                              // Search through xml using xpath and populate the book object
                              while (nodes1.MoveNext() && nodes2.MoveNext() && nodes3.MoveNext() )
0
Comment
Question by:ianinspain
  • 2
  • 2
4 Comments
 

Author Comment

by:ianinspain
ID: 17864945
Oh .. almost forgot ... i am using VS2003 as i know VS2005 uses different system classes for xml..

0
 
LVL 3

Accepted Solution

by:
boethius78 earned 500 total points
ID: 17865134
I've not really used the XPathNavigator in anger, but I think you'll be better off if you use .MoveToFirstChild(), then call .MoveNext() until there are no more sibling elements.  After that, call MoveToParent(), and continue around the loop from there.

Have a look at this, if you're not familiar with the methods: http://msdn2.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator_methods.aspx
0
 
LVL 3

Expert Comment

by:boethius78
ID: 17865142
Sorry, I meant XPathNavigator.MoveToNext().  XPathNavigator.MoveNext() doesn't exist...
0
 

Author Comment

by:ianinspain
ID: 17865281
thanks...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now