Solved

500pts: Help with crawling an xml file

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why does my excel data start at B1 instead of A1. 9 83
C# winforms programmitically move panels 6 29
transaction in asp.net, sql server 6 33
C# LINQ ForEach() question 6 28
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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 …

772 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