Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading XML file
#1
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.
#2
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>.
My Blog | My Setup | My Videos | Have a wonderful day.
#3
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?
#4
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!
My Blog | My Setup | My Videos | Have a wonderful day.
#5
I don't know how, but you just keep saving me! Thank you!
#6
Of course!
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with PCX file. Worf 2 14,888 10-15-2014, 10:41 AM
Last Post: Worf
  Protecting a file Covert2String 4 16,287 05-10-2012, 12:01 AM
Last Post: Covert2String
  Create a new file Covert2String 5 19,967 04-11-2012, 02:47 PM
Last Post: brandonio21
  Apply a diff file to another file Covert2String 8 29,274 04-09-2012, 03:21 PM
Last Post: Covert2String

Forum Jump:


Users browsing this thread: 1 Guest(s)