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
Solved

500pts: Help with crawling an xml file

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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