Solved

500pts: Help with crawling an xml file

Posted on 2006-11-03
4
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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