Reading XML in VB.NET

BlackCell1984

New member
Joined
Jul 5, 2006
Messages
2
i have writen a write XML sub

Code:
    Private Sub WriteOpts_XML()
        Dim XMLobj As Xml.XmlTextWriter
        Dim ue As New System.[Text].UnicodeEncoding
        XMLobj = New Xml.XmlTextWriter("Opts.xml", ue)
        XMLobj.Formatting = Xml.Formatting.Indented
        XMLobj.Indentation = 3
        XMLobj.WriteStartDocument()
        XMLobj.WriteStartElement("EthylOpts")
        XMLobj.WriteAttributeString("PunishValue", cmbPun.SelectedIndex)
        XMLobj.WriteAttributeString("BanSetValue", cmbBan.SelectedIndex)
        XMLobj.WriteStartElement("Options")
        XMLobj.WriteAttributeString("Nickname", CFNick.Text)
        XMLobj.WriteAttributeString("PunishTime", txtpun.Text)
        XMLobj.WriteAttributeString("BanSettings", txtban.Text)
        XMLobj.WriteEndElement()
        XMLobj.Close()
    EndSub

it gives out this XML file

Code:
<?xml version="1.0" encoding="utf-16"?>
<EthylOpts PunishValue="6" BanSetValue="2">
   <Options Nickname="BlackCell" PunishTime="TextBox1" BanSettings="TextBox1" />
</EthylOpts>

now i cant get s read XML to read the XML file and put those values and text where they are supposed to be.
 
Something like the following should work for you, it is however by no means the only way of doing it. Im a C# user myself, hopefully I translated it to vb correctly.
Code:
        Dim reader As New System.Xml.XmlTextReader("path")
        While reader.Read()
            If reader.NodeType = System.Xml.XmlNodeType.Element Then
                If reader.Name = "EthylOpts" Then
                    cmbPun.SelectedIndex = Single.Parse(reader.GetAttribute("PunishValue"))
                    cmbBan.SelectedIndex = Single.Parse(reader.GetAttribute("BanSetValue"))
                ElseIf reader.Name = "Options" Then
                    CFNick.Text = reader.GetAttribute("NickName")
                    txtpun.Text = reader.GetAttribute("PunishTime")
                    txtban.Text = reader.GetAttribute("BanSettings")
                End If
            End If
        End While
 
Back
Top