Hello, I am using XMLReader to look thru and XML file that I have. While I have been able to play around with it and get it to pull some of the information I want, I am wondering what commands to use to do what I really want.

For example, I really just want one line of information in the XML File. it uses <key> tags for the name of a string that follows it, heres an example,

<key>Serial Number</key>
<string>1234567890</string>

this format is used for other parameters also, but I just need the actual string of serial numbers. Is there a way to tell the reader to search for "Serial Number" and then read back the next line, which corresponds to what I want.

Just so you know I have been using the reader.name command to find string tags which is great, but there are 4 in the file im looking at, I just need the one that says "Serial Number" as its content.

Thanks for any help you can give me,

fwj

See if this helps.

Public Class Form1
    Private myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xmlDoc As New Xml.XmlDocument, ndKeyList, ndStringList As Xml.XmlNodeList, iNodeIndex As Integer
        With xmlDoc '// load File and lists.
            .Load(myFolder & "test.xml") : ndKeyList = .GetElementsByTagName("key") : ndStringList = .GetElementsByTagName("string")
        End With
        For i As Integer = 0 To ndKeyList.Count - 1 '// loop thru key list.
            If ndKeyList(i).InnerText = "Serial Number" Then
                iNodeIndex = i '// set the Index where located.
                Exit For '// exit.loop since done.
            End If
        Next
        MsgBox(ndStringList(iNodeIndex).InnerText) '// display string at the same Index as the key.
    End Sub
End Class

Worked out how to do what I needed, figured someone might find the code useful

 'loads the xmldocument

        Dim XMLDoc As New XmlDocument

        Dim redsets = New XmlReaderSettings()

        redsets.XmlResolver = Nothing

        redsets.DtdProcessing = DtdProcessing.Ignore



        XMLDoc.Load(FileLocation)

        'selects the nodes to read at the specified xml path - check xml file for specific path

        Dim MyNodes = XMLDoc.SelectNodes("plist/dict/dict/dict/key")

        'for each node that it finds where the inner text is "Serial Number"(or whatever you want) it reads the next nodes inner text value.

        For Each Node As XmlNode In MyNodes

            If Node.InnerText = "Serial Number" Then

                SerialNumberBox.Text = (Node.NextSibling.InnerText)

            End If

        Next

So to briefly explain i used DTDprocessing settings because the xml file was formatted that way, i found out that if you parse or otherwise try to use the file settings it may try to 'talk' to something else e.g. my original file had a website written into the DTD part and crashed the attempt to read the file if the computer wasn't online.

The xml layout was a 'plist' file so not exactly like regular xml, but you can see how i used the xml path to specify what nodes i was looking for. Then i used code to find a certain node that had a specific file string.

Then i used the 'NextSibling' command to grab the value that was next in the file - this was actually a dynamic value for the inner text which is why i had to go the long way around.

Hope this helps others in manipulating data from an xml file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.