Solved

500pts: Help with crawling an xml file

Posted on 2006-11-03
4
264 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
C# replace string in void with dR["myData"].ToString() 3 42
Error when loading the database 16 58
SQL Login 17 40
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…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

23 Experts available now in Live!

Get 1:1 Help Now