Dumdidum
asked on
XML - how to read this XML-File?
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("Col lectionTyp e").InnerT ext
TextBox2.Text = node.SelectSingleNode("Col lectionNum ber").Inne rText
TextBox3.Text = node.SelectSingleNode("ID" ).InnerTex t
TextBox4.Text = node.SelectSingleNode("Ove rview").In nerText
Err.Clear()
End Sub
Private Sub ComboBox1_SelectionChangeC ommitted(B yVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeC ommitted
Sven(ComboBox1.SelectedInd ex + 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</Col lectionTyp e>
<CollectionNumber>408</Col lectionNum ber>
<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</Col lectionTyp e>
<CollectionNumber>409</Col lectionNum ber>
<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
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.
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("/
TextBox1.Text = node.SelectSingleNode("Col
TextBox2.Text = node.SelectSingleNode("Col
TextBox3.Text = node.SelectSingleNode("ID"
TextBox4.Text = node.SelectSingleNode("Ove
Err.Clear()
End Sub
Private Sub ComboBox1_SelectionChangeC
Sven(ComboBox1.SelectedInd
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</Col
<CollectionNumber>408</Col
<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</Col
<CollectionNumber>409</Col
<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
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot...
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[Ti
' Extract the text into the desired format
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder(
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