[Help]XmlReader

Posts 1–6 of 6 · Page 1 of 1
[Help]XmlReader
Writer:

Code:
    Private Sub _XMLWriter()
        Try
            Dim enc As New System.Text.UnicodeEncoding

            Dim XMLobj As Xml.XmlTextWriter _
              = New Xml.XmlTextWriter(Application.StartupPath & "\Data.xml", enc)

            With XMLobj

                .Formatting = Xml.Formatting.Indented
                .Indentation = 4

                .WriteStartDocument()

                'general settings

                .WriteStartElement("Data")
                .WriteAttributeString("Servername", gen_name.Text)
                .WriteAttributeString("ServerIP", gen_ip.Text)
                .WriteAttributeString("RconPassword", gen_recon.Text)
                .WriteAttributeString("ServerPort", gen_port.Text)
                .WriteAttributeString("PlayerLimit", gen_player.Text)
                .WriteAttributeString("Private Slots", gen_private.Text)
                .WriteAttributeString("AdminName", gen_admin.Text)
                .WriteAttributeString("Email", gen_email.Text)
                .WriteAttributeString("WebsiteURL", gen_website.Text)
                .WriteAttributeString("Location", gen_location.Text)
                .WriteAttributeString("ModName", gen_modname.Text)
                .WriteAttributeString("ModVersion", gen_modver.Text)
                .WriteAttributeString("ModURL", gen_modurl.Text)
                .WriteAttributeString("WebsiteURL", gen_mapurl.Text)

                'gametype settings

                .WriteAttributeString("TDMScore", tdm_score.Text)
                .WriteAttributeString("TDMTime", tdm_time.Text)
                .WriteAttributeString("TDMRound", tdm_round.Text)
                .WriteAttributeString("TDMLives", tdm_lives.Text)
                .WriteAttributeString("TDMRespawn", tdm_respawn.Text)

                .WriteAttributeString("FFAScore", ffa_score.Text)
                .WriteAttributeString("FFATime", ffa_time.Text)
                .WriteAttributeString("FFARound", ffa_round.Text)
                .WriteAttributeString("FFALives", ffa_lives.Text)
                .WriteAttributeString("FFARespawn", ffa_respawn.Text)

                .WriteAttributeString("DOMScore", dom_score.Text)
                .WriteAttributeString("DOMTime", dom_time.Text)
                .WriteAttributeString("DOMRound", dom_round.Text)
                .WriteAttributeString("DOMLives", dom_lives.Text)
                .WriteAttributeString("DOMRespawn", dom_respawn.Text)

                .WriteAttributeString("KOTHScore", koth_score.Text)
                .WriteAttributeString("KOTHTime", koth_time.Text)
                .WriteAttributeString("KOTHRound", koth_round.Text)
                .WriteAttributeString("KOTHLives", koth_lives.Text)
                .WriteAttributeString("KOTHRespawn", koth_respawn.Text)

                .WriteAttributeString("SABScore", sab_score.Text)
                .WriteAttributeString("SABTime", sab_time.Text)
                .WriteAttributeString("SABRound", sab_round.Text)
                .WriteAttributeString("SABLives", sab_lives.Text)
                .WriteAttributeString("SABRespawn", sab_respawn.Text)

                .WriteAttributeString("SDBombtimer", sd_bomb.Text)
                .WriteAttributeString("SDPlant", sd_plant.Text)
                .WriteAttributeString("SDDefuse", sd_defuse.Text)
                .WriteAttributeString("SDTime", sd_time.Text)
                .WriteAttributeString("SDRound", sd_round.Text)

                'map rotation
                .WriteAttributeString("MapRotation", maprotate.Text)

                'mod

                .WriteAttributeString("NameofModFolder", modname.Text)

                'other settings
                .WriteAttributeString("ServerPassword", serverpw.Text)
                .WriteAttributeString("PrivatePassword", privatepw.Text)
                .WriteAttributeString("MaxRate", maxrate.Text)
                .WriteAttributeString("FPS", fps.Text)
                .WriteAttributeString("MaxPing", maxping.Text)
                .WriteAttributeString("DropInactivePlayers", dropinactive.Text)
                .WriteAttributeString("TimeoutForTeamkiller", teamkiller.Text)
                .WriteAttributeString("DurationOfTempBan", tempbann.Text)
                .WriteAttributeString("MaxHealth", maxhealth.Text)
                .WriteAttributeString("HealthRegeneration", healthreg.Text)
                .WriteAttributeString("Suiciderespawn", suiciderespawn.Text)

                'start server
                .WriteAttributeString("Multiplayer.ExeDirectory", mpexe.Text)
                .WriteAttributeString("CommandLine", commandline.Text)
                .WriteAttributeString("Nameofconfig", configname.Text)

                .WriteAttributeString("Commands", rbcommands.Text)

                .WriteEndElement()
                .Close()

            End With
        Catch
        End Try

    End Sub

Reader:

Code:
 Private Function XMLReader(ByVal Element As String, ByVal Feldname As String)
        Dim value As String = 0

        Dim _XMLReader As Xml.XmlReader _
          = New Xml.XmlTextReader(Application.StartupPath & "\Data.xml")

        With _XMLReader
            Try
                Do While .Read
                    Select Case .NodeType
                        Case Xml.XmlNodeType.Element
                            If .Name = Element Then
                                If .AttributeCount > 0 Then
                                    While .MoveToNextAttribute
                                        If .Name = Feldname Then
                                            value = .Value
                                        End If
                                    End While
                                End If
                            End If
                    End Select
                Loop
            Catch
            End Try
            .Close()
        End With


        If value = String.Empty Then
            Return ""
        Else
            Return value
        End If

    End Function

Reading the xml entries:

Code:
    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

        'general settings

        gen_name.Text = XMLReader("Data", "Servername")
        gen_ip.Text = XMLReader("Data", "ServerIP")
        gen_recon.Text = XMLReader("Data", "RconPassword")
        gen_port.Text = XMLReader("Data", "ServerPort")
        gen_player.Text = XMLReader("Data", "PlayerLimit")
        gen_private.Text = XMLReader("Data", "PrivateSlots")
        gen_admin.Text = XMLReader("Data", "AdminName")
        gen_email.Text = XMLReader("Data", "Email")
        gen_website.Text = XMLReader("Data", "WebsiteURL")
        gen_location.Text = XMLReader("Data", "Location")
        gen_modname.Text = XMLReader("Data", "ModName")
        gen_modver.Text = XMLReader("Data", "ModVersion")
        gen_modurl.Text = XMLReader("Data", "ModURL")
        gen_mapurl.Text = XMLReader("Data", "WebsiteURL")

        'game types
        'tdm
        tdm_score.Text = XMLReader("Data", "TDMScore")
        tdm_lives.Text = XMLReader("Data", "TDMLives")
        tdm_round.Text = XMLReader("Data", "TDMRound")
        tdm_time.Text = XMLReader("Data", "TDMTime")
        tdm_respawn.Text = XMLReader("Data", "TDMRespawn")

        'ffa
        ffa_score.Text = XMLReader("Data", "FFAScore")
        ffa_lives.Text = XMLReader("Data", "FFALives")
        ffa_round.Text = XMLReader("Data", "FFARound")
        ffa_time.Text = XMLReader("Data", "FFATime")
        ffa_respawn.Text = XMLReader("Data", "FFARespawn")

        'dom
        dom_score.Text = XMLReader("Data", "DOMScore")
        dom_lives.Text = XMLReader("Data", "DOMLives")
        dom_round.Text = XMLReader("Data", "DOMRound")
        dom_time.Text = XMLReader("Data", "DOMTime")
        dom_respawn.Text = XMLReader("Data", "DOMRespawn")

        'koth
        koth_score.Text = XMLReader("Data", "KOTHScore")
        koth_lives.Text = XMLReader("Data", "KOTHLives")
        koth_round.Text = XMLReader("Data", "KOTHRound")
        koth_time.Text = XMLReader("Data", "KOTHTime")
        koth_respawn.Text = XMLReader("Data", "KOTHRespawn")

        'sab
        sab_score.Text = XMLReader("Data", "SABScore")
        sab_lives.Text = XMLReader("Data", "SABLives")
        sab_round.Text = XMLReader("Data", "SABRound")
        sab_time.Text = XMLReader("Data", "SABTime")
        sab_respawn.Text = XMLReader("Data", "SABRespawn")

        'sd
        sd_bomb.Text = XMLReader("Data", "SDBombtimer")
        sd_plant.Text = XMLReader("Data", "SDPlant")
        sd_defuse.Text = XMLReader("Data", "SDDefuse")
        sd_round.Text = XMLReader("Data", "SDRound")
        sd_time.Text = XMLReader("Data", "SDTime")


        'map rotation
        rb_maprotation.Text = XMLReader("Data", "MapRotation")

        'mod name
        modname.Text = XMLReader("Data", "NameofModFolder")


        'other settings

        serverpw.Text = XMLReader("Data", "ServerPassword")
        privatepw.Text = XMLReader("Data", "Private Password")
        maxrate.Text = XMLReader("Data", "MaxRate")
        fps.Text = XMLReader("Data", "FPS")
        maxping.Text = XMLReader("Data", "MaxPing")
        dropinactive.Text = XMLReader("Data", "DropInactivePlayers")
        teamkiller.Text = XMLReader("Data", "TimeoutForTeamkiller")
        tempbann.Text = XMLReader("Data", "DurationOfTempBan")
        maxhealth.Text = XMLReader("Data", "MaxHealth")
        healthreg.Text = XMLReader("Data", "HealthRegeneration")
        suiciderespawn.Text = XMLReader("Data", "SuicideRespawn")


        'start server

        mpexe.Text = XMLReader("Data", "Multiplayer.ExeDirectory")
        commandline.Text = XMLReader("Data", "CommandLine")
        configname.Text = XMLReader("Data", "Nameofconfig")

        rbcommands.Text = XMLReader("Data", "Commands")
    End Sub


HOWEVER, the textbox just shows a "0" as entry, even though there are other entries in the .xml...

I hope you can help me out

Thaaaaaaaankies
A. I can't speak for everyone else, but it's alot to read through the code, screenshot and the part of the code that is problematic is the best approach (IMO)

B. The Code is sloppy (IMO, not being offensive), So whatever you can do to explain the issue more, would really help us help you.
Quote Originally Posted by NextGen1 View Post
A. I can't speak for everyone else, but it's alot to read through the code, screenshot and the part of the code that is problematic is the best approach (IMO)

B. The Code is sloppy (IMO, not being offensive), So whatever you can do to explain the issue more, would really help us help you.
Basically what he said ^.^
Sorry, it was late and I was tired

Well, here's a video showing the problem:



There isn't any error at all.
There isn't a video either.......

Edit: Sorry was on Iphone.... No flash ( I forgot) ..

So it will write, but not open?
[Had to double post , sorry]

I am working on this now,
First a few errors in your code that need to be corrected

Code:
                .WriteAttributeString("Servername", gen_name.Text)
                .WriteAttributeString("ServerIP", gen_ip.Text)
                .WriteAttributeString("RconPassword", gen_recon.Text)
                .WriteAttributeString("ServerPort", gen_port.Text)
                .WriteAttributeString("PlayerLimit", gen_player.Text)
                .WriteAttributeString("Private Slots", gen_private.Text)
                .WriteAttributeString("AdminName", gen_admin.Text)
                .WriteAttributeString("Email", gen_email.Text)
                .WriteAttributeString("WebsiteURL", gen_website.Text)
                .WriteAttributeString("Location", gen_location.Text)
                .WriteAttributeString("ModName", gen_modname.Text)
                .WriteAttributeString("ModVersion", gen_modver.Text)
                .WriteAttributeString("ModURL", gen_modurl.Text)
                .WriteAttributeString("WebsiteURL", gen_mapurl.Text)
In this code tag you have two problems,

1. You are writing Player (space) Slots
In XML a Space is unexpected, You are reading

Code:
                .WriteAttributeString("PrivateSlots", gen_private.Text)
Note how there is no space, the way it should be, so remove the space.

You mustn't try to move your space with your mind... that.. is impossible... First, you need to realize............................ There is no space.

2. Redundent , There will be a part of the code the breaks (options strict and a break point on button9 click event) with having 2 "WebsiteUrl"
Remove one of them


More to come

Another problem with the way your writing the XML

Private is being saved with a space after it (it shouldn't) It is being saved as
Private ="" , It should be Private=""

You should fix that too.

More to come

Fixed it

Add this to button9

Code:
 Dim value As String = "0"
        Dim doc As New XmlDocument
        doc.Load(Application.StartupPath & "\Data.xml")
Above this

Code:
   'general settings

        gen_name.Text = XMLReader("Data", "Servername")
        gen_ip.Text = XMLReader("Data", "ServerIP")
        gen_recon.Text = XMLReader("Data", "RconPassword")
        gen_port.Text = XMLReader("Data", "ServerPort")
        gen_player.Text = XMLReader("Data", "PlayerLimit")
        gen_private.Text = XMLReader("Data", "PrivateSlots")
        gen_admin.Text = XMLReader("Data", "AdminName")
        gen_email.Text = XMLReader("Data", "Email")
        gen_website.Text = XMLReader("Data", "WebsiteURL")
        gen_location.Text = XMLReader("Data", "Location")
        gen_modname.Text = XMLReader("Data", "ModName")
        gen_modver.Text = XMLReader("Data", "ModVersion")
        gen_modurl.Text = XMLReader("Data", "ModURL")
        gen_mapurl.Text = XMLReader("Data", "WebsiteURL")
Works fine

Note: I suggest anyone working with XML get your hands on a copy of front page and use the "Verify XML" feature, it will find errors like the one above, So you can fix them in your code, this is unrelated to why your code wasn't working, but it will cause more errors now that the code is fixed if you do not take care of it.
Posts 1–6 of 6 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?