BP Forums
Reading XML file - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+---- Thread: Reading XML file (/showthread.php?tid=521)



Reading XML file - Covert2String - 04-22-2012

Hi, this time I need to read values from a XML file, the problem is, it pops up an error saying it contains invalid characters. These are a few lines of my XML file:
Quote:<item_proto extended="true" version="1">
<Item vnum="1" name="Yang" type="9" subtype="0" weight="0" size="1" antiflag="0" flag="0" wearflag="0" immuneflag="0" gold="0" buy_price="0" limittype0="0" limitvalue0="0" limittype1="0" limitvalue1="0" applytype0="0" applyvalue0="0" applytype1="0" applyvalue1="0" applytype2="0" applyvalue2="0" value0="0" value1="0" value2="0" value3="0" value4="0" value5="0" socket0="0" socket1="64864" socket2="127" socket3="65008" socket4="21631" socket5="4855" refine_vnum="0" refine_set="0" magic_pct="0" specular="0" socket_pct="0" />

So, how can I fully read the XML file, line by line, and fill a Box (comboBox, listbox...) with the values.


Re: Reading XML file - brandonio21 - 04-23-2012

Covert2String Wrote:the problem is, it pops up an error saying it contains invalid characters.

When does this error popup? This could definitely be a problem!


Also: This XML File does not seem to be written using standard XML syntax, which could make things difficult. Here is an example of standard XML Syntax:
Code:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <Table>
    <Product>
    <Product_id value="1"/>
    <Product_name value="Product 1"/>
    <Product_price value="1000"/>
    </Product>
    <Product>
    <Product_id value="2"/>
    <Product_name value="Product 2"/>
    <Product_price value="5000"/>
    </Product>
    </Table>.



Re: Reading XML file - Covert2String - 04-29-2012

So, it's not a valid XML file, however I still need to read it, like a custom XML parser, any thoughts on how to make the code not too slow?


Re: Reading XML file - brandonio21 - 04-30-2012

So here is my method of parsing the text file. The form contains two listboxes (1 and 2), 1 contains the categories while 2 contains the actual values.

Here is the code I used:
Code:
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim reader As New System.IO.StreamReader("C:\Users\Brandon\Desktop\lol.txt") 'Read the text file

        Dim i As Integer = 0 'Keep track if the item is a category or a value
        For Each entry In reader.ReadToEnd.Split(Chr(34)) 'Split by quotation marks

            'Here we check to see if the value or category contains invalid symbols that are
            'not necessarily pertinent to the information that we need to gather.
            'If the string contains these symbols, they are removed.
            If (entry.Contains("<") Or entry.Contains(">") Or entry.Contains(" ") Or entry.Contains("/") Or entry.Contains("\")) Then
                Try
                    entry = entry.Replace("<", "")
                    entry = entry.Replace(">", "")
                    entry = entry.Replace(" ", "")
                    entry = entry.Replace("/", "")
                    entry = entry.Replace("\", "")
                Catch ex As Exception
                    Continue For
                End Try
            End If


            If (i Mod 2 = 0) Then 'The item is a category
                ListBox1.Items.Add(entry)
            Else 'The item is a value
                ListBox2.Items.Add(entry)
            End If

            i += 1 'Increase the counter of the category determiner so we can keep track
        Next

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        ListBox2.SelectedIndex = ListBox1.SelectedIndex
    End Sub
End Class

You may find it easier to view the code on PasteBin:
<!-- m --><a class="postlink" href="http://pastebin.com/M7R1FZaK">http://pastebin.com/M7R1FZaK</a><!-- m -->

Hopefully this helps!


Re: Reading XML file - Covert2String - 04-30-2012

I don't know how, but you just keep saving me! Thank you!


Re: Reading XML file - brandonio21 - 04-30-2012

Of course!