Solved

XML - how to read this XML-File?

Posted on 2004-08-29
4
194 Views
Last Modified: 2010-04-23
Hi all

I have this code here as proposed by the Microsoft XML-Video and it's working:

    Dim xmlfile As String = "..\collection.xml"
    Dim xmldoc As XmlDocument

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim xmltr As New XmlTextReader(xmlfile)
        While xmltr.Read
            If xmltr.Name = "Title" AndAlso xmltr.NodeType = XmlNodeType.Element Then
                ComboBox1.Items.Add(xmltr.ReadString)
            End If
        End While
        xmltr.Close()

        xmldoc = New XmlDocument
        xmldoc.Load(xmlfile)

    End Sub

    Sub Sven(ByVal position As Integer)
        On Error Resume Next
        TextBox1.Text = "-"
        TextBox2.Text = "-"
        TextBox5.Text = "-"
        TextBox3.Text = "-"
        Dim node As XmlNode = xmldoc.SelectSingleNode("/Collection/DVD[" & position & "]")
        TextBox1.Text = node.SelectSingleNode("CollectionType").InnerText
        TextBox2.Text = node.SelectSingleNode("CollectionNumber").InnerText
        TextBox3.Text = node.SelectSingleNode("ID").InnerText
        TextBox4.Text = node.SelectSingleNode("Overview").InnerText

        Err.Clear()
    End Sub

    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
        Sven(ComboBox1.SelectedIndex + 1)

    End Sub



Now the problem is that I have this in my XML-File:

<Collection>
<DVD>
  <Title>Austin Powers: Spion in geheimer Missionarsstellung</Title>
    <Regions>
        <Region>2</Region>
        <Region>1</Region>
        <Region>6</Region>
    </Regions>
    <CollectionType>Owned</CollectionType>
    <CollectionNumber>408</CollectionNumber>
    <Overview>Mike Myers ist Powers - Austin Powers, </Overview>
  </DVD>
<DVD>
  <Title>Austin Powers</Title>
    <Regions>
        <Region>2</Region>
        <Region>4</Region>
        <Region>3</Region>
    </Regions>
    <CollectionType>Owned</CollectionType>
    <CollectionNumber>409</CollectionNumber>
    <Overview>Mike Myers ist Austin Powers</Overview>
  </DVD>
</Collection>



Now I want all Regions of one DVDs in a Textbox like:

Textbox1 = "2#1#6"

How can I do this? Should be in "Sub Sven"

Thanks a lot!!!!

Sven Rutten
0
Comment
Question by:Dumdidum
  • 3
4 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 11926233
Here's a function that will generate the string you indicate from your XML.  You'll have to modify it to behave the way you want.  If this is not what you had in mind, let me know and I'll adjust it.

    Function ReadXML(ByVal xmlFile As String, ByVal title As String) As String
        ' Open and load the doc - you could pass an open doc as a parameter
        Dim xdoc As System.Xml.XmlDocument = New System.Xml.XmlDocument
        xdoc.Load(xmlFile)

        'Select Region text nodes under the desired title
        Dim nl As System.Xml.XmlNodeList = xdoc.SelectNodes("//DVD[Title = '" + title + "']//Region/text()")

        ' Extract the text into the desired format
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder(16)
        For Each region As System.Xml.XmlNode In nl
            sb.Append(region.Value)
            sb.Append("#")
        Next
        ' Remove the trailing #
        sb.Remove(sb.Length - 1, 1)

        ReadXML = sb.ToString()
    End Function
0
 
LVL 19

Expert Comment

by:drichards
ID: 11926239
You would call the above function like this:

    Dim tbText = ReadXML("XMLFile1.xml", "Austin Powers: Spion in geheimer Missionarsstellung")

This will get the region info for "Austin Powers: Spion in geheimer Missionarsstellung" if your XML file is named XMLFile1.xml.
0
 
LVL 19

Accepted Solution

by:
drichards earned 100 total points
ID: 11926288
Didn't read the question closely enough.  Here's what I believe you were looking for:
 
   Sub Sven(ByVal position As Integer)
        On Error Resume Next
        TextBox1.Text = "-"
        TextBox2.Text = "-"
        TextBox5.Text = "-"
        TextBox3.Text = "-"
        Dim node As XmlNode = xmldoc.SelectSingleNode("/Collection/DVD[" & position & "]")
        TextBox1.Text = node.SelectSingleNode("CollectionType").InnerText
        TextBox2.Text = node.SelectSingleNode("CollectionNumber").InnerText
        TextBox3.Text = node.SelectSingleNode("ID").InnerText
        TextBox4.Text = node.SelectSingleNode("Overview").InnerText

        Dim nl As System.Xml.XmlNodeList = xdoc.SelectNodes("//DVD[" & position & "]//Region/text()")

        ' Extract the text into the desired format
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder(16)
        For Each region As System.Xml.XmlNode In nl
            sb.Append(region.Value)
            sb.Append("#")
        Next
        ' Remove the trailing #
        sb.Remove(sb.Length - 1, 1)

        TextBox1.Text = sb.ToString()
 
       Err.Clear()
    End Sub

0
 

Author Comment

by:Dumdidum
ID: 11927376
Thanks a lot...

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

12 Experts available now in Live!

Get 1:1 Help Now