XML Parsing Prob

samboy29

Member
Joined
Oct 2, 2008
Messages
8
Programming Experience
Beginner
Hello, I hope I'm in the right place, but I was just wondering what is wrong with this code, as it only finds the parses and saves one word, so I figure something in the for section is incorrect.

It will go through the for section, go through the if statements, which I have the 'greeting' component as being found as true. However, it only saves the first word it finds with the tag 'greeting'. The same applies when parsing other tags such as 'food' (I've removed most below because I don't believe I need to have it all there). Thanks for any help in advance,

and yes, I am a beginner :(

Edit: to clarify, everything seems to be working fine except that when going through the for section of the code, it only saves one word, instead of about 30 words which have the tag 'greeting'.

And m_node.ChildNodes.Item(4) is the node that contains the tag. Childnodes0 and 1 contain the text that I want to retrieve and save.

VB.NET:
Sub ParseGetPositiveTags()
        Try
            ListLength = 0
            Dim m_xmld As XmlDocument
            Dim m_nodelist As XmlNodeList
            Dim m_node As XmlNode
            'Create the XML Document

            m_xmld = New XmlDocument()
            'Load the Xml file

            m_xmld.Load("C:\Users\Sam\Dictionary.xml")
            'Get the list of  nodes 

            m_nodelist = m_xmld.SelectNodes("/Dictionary/Word")
            'Loop through the nodes
            Dim p As Integer
            p = 1

            For Each m_node In m_nodelist

                If m_node.ChildNodes.Item(4).InnerText = "Greeting" And ListOption.Greetings = True Then
                    SWordList(p) = m_node.ChildNodes.Item(0).InnerText
                    EWordList(p) = m_node.ChildNodes.Item(1).InnerText

                    p = p + 1
                    ListLength = ListLength + 1
                End If

                If m_node.ChildNodes.Item(4).InnerText = "Food" And ListOption.Food = True Then
                    SWordList(p) = m_node.ChildNodes.Item(0).InnerText
                    EWordList(p) = m_node.ChildNodes.Item(1).InnerText
                    p = p + 1
                    ListLength = ListLength + 1
                End If

            Next


        Catch errorVariable As Exception
            'Error trapping
            MsgBox(errorVariable.ToString())
        End Try
    End Sub
 
Last edited:
Samboy, would it be possible for you to post a snip from the XML file so I can see the structure better?

Also, can you confirm your platform of Studio 2008/3.5 is accurate? I believe I can help you with a very easy solution given these two things.
 
Sure, code snippet of the XML File:

HTML:
<?xml version="1.0" encoding="UTF-8"?>
<Dictionary>
    <Word>
      <SpanishWord>¿cómo está?</SpanishWord>
      <EnglishWord>How are you?</EnglishWord>
      <Pronunciation></Pronunciation>
      <Definition></Definition>
      <Tags>Greeting</Tags>
    </Word>

    <Word>
        <SpanishWord>¿qué tal?</SpanishWord>
        <EnglishWord>What's up?</EnglishWord>
        <Pronunciation></Pronunciation>
        <Definition></Definition>
        <Tags>Greeting</Tags>
    </Word>
...
 </Dictionary>

I am using Visual Basic 2008 Express Edition.

Thanks.
 
Alright, so along with Studio 2008 comes the ability to use LINQ over XML. This gives you a powerful new way to use XML documents over the previous DOM models.

So, you could restate your code approxamately as follows:

VB.NET:
Dim ListOption As String = "Greeting"
Dim EWordList As New List(Of String)
Dim SWordList As New List(Of String)

Dim xmlInput As XDocument
xmlInput = XDocument.Load([YOUR PATH]"\Text.xml")

Dim wordsReturned = From word In xmlInput...<Dictionary>.<Word> _
                            Where word.<Tags>.Value = ListOption _
                            Select english = word.<EnglishWord>.Value, spanish = word.<SpanishWord>.Value

For Each word In wordsReturned
 EWordList.Add(word.english)
 SWordList.Add(word.spanish)
Next

To give a brief break down:

I first declare just placeholder variables to match what I could see from your application, these may be different types or what not in your application, but I needed something so I guessed ;-).

After this we load the XML document (replace [YOUR PATH]).

Next, we execute a LINQ query over the XML document. This query says:
For each occurrence of <Dictionary>.<Word> where that elements <Tags>.Value is equal to ListOption select into memory the new variable English as being equal to the current elements <EnglishWord>.Value; then do the same for Spanish.

Finally, go through each word returned from the LINQ query, adding the English and Spanish words to their respective lists.

It's a new approach unique (currently) to VB.NET in Studio 2008 but its totally elegant. If you need more help understanding LINQ check here:
Visual Basic How Do I Video Series (scroll down about 1/4 for LINQ videos), or post back here for more help!
 
wow, thanks that is neat :D I'll have to check out those vids, LINQ seems pretty sweet, although I don't fully understand what it is at the moment ;) Thanks for the help!

Cheers
 
Yes sir.

Just remember, LINQ is basically a new way to interact with data stored in any type of object.

Where previously you could only do 'queries' over SQL databases, now you can do them over anything LINQ can interface to (which is most everything).

If you need more helping understanding LINQ for your current question in this thread, or as you begin to use it more (hopefully!) feel free to post back in these forums!
 
Yes sir.

Just remember, LINQ is basically a new way to interact with data stored in any type of object.

Where previously you could only do 'queries' over SQL databases, now you can do them over anything LINQ can interface to (which is most everything).

If you need more helping understanding LINQ for your current question in this thread, or as you begin to use it more (hopefully!) feel free to post back in these forums!

Ahh, makes sense, and I will be sure to. I've downloaded all of the LINQ related vids from MSDN (which seems to have heaps of useful stuff, thanks for linking me to it!) which I will have to check out when I get the time (Uni starts up again in a couple days, and unfortunately my course is in no way related to programming).

Thanks again for all your help.
 

Latest posts

Back
Top