Brain Quit working, XML read problem

Nezo

Member
Joined
Sep 27, 2005
Messages
12
Programming Experience
3-5
Alright so folling a few tutorials and my school book I was able to read and write several values from XML files, however I came to a problems when I wanted to add the follwing values into the file

VB.NET:
<Characters> 
<Character Code="Nezo">
	 <Name>Nezo</Name>
	 <Level>5</Level>
	 <Strength>100</Strength>
	 <Dexterity>30</Dexterity>
	 <Vitality>30</Vitality>
	 <Energy>40</Energy>
	 <Items>
		 <Item>Short Sword</Item>
		 <Item>Other Item</Item>
	 </Items>
</Character>
</Characters>

I basically want to add every instance of 'Item' into an array Character.Items the following code is what I've tried along with 100 other things and I've figured I'm missing something that just isn't written in this book or my brain just doesn't want to comprehend the logic needed to complete this task.

VB.NET:
Private Function CharactersLoaded() As Boolean 
Dim CharacterReader As New XmlTextReader(sCharPath)
CharacterReader.WhitespaceHandling = WhitespaceHandling.None
Try
Do Until CharacterReader.Name = "Character"
CharacterReader.Read()
Loop
Do While CharacterReader.Name = "Character"
Character.Code = CharacterReader.Item("Code")
CharacterReader.ReadStartElement("Character")
Character.Name = CharacterReader.ReadElementString("Name")
Character.Level = CharacterReader.ReadElementString("Level")
Character.Strength = CharacterReader.ReadElementString("Strength")
Character.Dexterity = CharacterReader.ReadElementString("Dexterity")
Character.Vitality = CharacterReader.ReadElementString("Vitality")
Character.Energy = CharacterReader.ReadElementString("Energy")
CharacterReader.ReadEndElement()
CharacterReader.ReadStartElement("Items")
Character.Item = CharacterReader.ReadElementString("Item")
CharacterReader.ReadEndElement()
Characters.Add(Character)
Loop
CharacterReader.Close()
Catch exml As XmlException
MessageBox.Show("There is an error in the XML file.", "XML Error")
Return False
End Try
Return True
End Function

Sorry for the poor formatting, not familar with this board yet, cannot really cut/paste neatly. But basically I've tried for/next loops in several variaties and 1000 other things I'm basically stuck, every time I get an exception error.
 
Been a while, just cleaning up this thread.. :)
I would opt for this approach, note that I just added a simple character class to fit in with your code sample.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] somecharacter
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] code, name, level, strength, dexterity, vitality, energy [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] items [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ArrayList
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] character [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] somecharacter
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] characters [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ArrayList[/SIZE]
[SIZE=2] 
[/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] CharactersLoaded() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xmlfilename [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"characters.xml"
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xdoc [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] XmlDocument
xdoc.Load(xmlfilename)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cs [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlNodeList = xdoc.SelectNodes([/SIZE][SIZE=2][COLOR=#800000]"Characters/Character"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] node [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlNode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] cs
character = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] somecharacter
character.code = node.Attributes([/SIZE][SIZE=2][COLOR=#800000]"Code"[/COLOR][/SIZE][SIZE=2]).InnerText
character.name = node([/SIZE][SIZE=2][COLOR=#800000]"Name"[/COLOR][/SIZE][SIZE=2]).InnerText
character.level = node.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"Level"[/COLOR][/SIZE][SIZE=2]).InnerText
character.strength = node.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"Strength"[/COLOR][/SIZE][SIZE=2]).InnerText
character.dexterity = node.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"Dexterity"[/COLOR][/SIZE][SIZE=2]).InnerText
character.vitality = node.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"Vitality"[/COLOR][/SIZE][SIZE=2]).InnerText
character.energy = node.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"Energy"[/COLOR][/SIZE][SIZE=2]).InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] item [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlNode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] node.SelectNodes([/SIZE][SIZE=2][COLOR=#800000]"Items/Item"[/COLOR][/SIZE][SIZE=2])
character.items.Add(item.InnerText)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]characters.Add(character)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]
 
Back
Top