XML Help...PLEASE!!!

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
7 posts Page 1 of 1
Contributors
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

XML Help...PLEASE!!!
Scottie1972
i am having some trouble trying to figure this one out. If you can help, please do.

my XML FILE
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<enemies>
  <enemie ID="1">
    <sector>100</sector>
    <playerName>Billy Bob</playerName>
    <level>1</level>
    <coords>00000,000</coords>
    <tactic>none</tactic>
  </enemie>
  <enemie ID="2">
    <sector>200</sector>
    <playerName>Billy Bob</playerName>
    <level>2</level>
    <coords>00000,000</coords>
    <tactic>none</tactic>
  </enemie>
  <enemie ID="3">
    <sector>300</sector>
    <playerName>Billy Bob</playerName>
    <level>3</level>
    <coords>00000,000</coords>
    <tactic>none</tactic>
  </enemie>
</enemies>
my Load Function
Code: Select all
Dim file As String = Application.StartupPath + "\data\enemies.xml"
                Dim xd As New XmlDocument()
                xd.Load(file)
                For Each IDNode As XmlNode In xd.SelectNodes("//enemies/enemie")
                    If IDNode.HasChildNodes = True Then
                        Dim str(6) As String
                        Dim itm As ListViewItem
                        str(0) = IDNode.ChildNodes(0).InnerText
                        str(1) = IDNode.ChildNodes(1).InnerText
                        str(2) = IDNode.ChildNodes(2).InnerText
                        str(3) = IDNode.ChildNodes(3).InnerText
                        str(4) = IDNode.ChildNodes(4).InnerText
                        'str(5) = IDNode.ChildNodes(5).InnerText
                        itm = New ListViewItem(str)
                        itm.Tag = ""
                        ListView1.Items.Add(itm)
                    End If
                Next
what i need todo is loop through all the nodes in the XML and load the results into a listview. so later i can click on a listing and edit that node/entry.

i cant do a regular loop without the ID="" attribute but i can edit the listing. i have no way of searching the xml for the correct record to update. ei: the ID="" attribute.

any ideas?

scottie1972
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: XML Help...PLEASE!!!
CodenStuff
Hello Scottie,

Im not 100% sure what you want to do and you know im no expert in XML lol.

If you mean you want to get the ID attribute of each enemy node then you could use:
Code: Select all
IDNode.Attributes.ItemOf(0).Value
That would show the ID attribute of each node. Is that what you meant? :?
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Re: XML Help...PLEASE!!!
Scottie1972
what i need todo is loop throught all the entries and placer them in a listview control. then be able to edit/update/save each entry by its ID="?"
attribute.
Image
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

Re: XML Help...PLEASE!!!
Kieken72
Code: Select all
Dim enemieID as integer
Using reader As XmlReader = XmlReader.Create(Application.StartupPath + "\data\enemies.xml")
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Name = "enemie" Then

                        
                        enemieID = reader("ID")
                       
                    End If
                End If
            End While
        End Using
;)
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Re: XML Help...PLEASE!!!
Scottie1972
Kieken72 wrote:
Code: Select all
Dim enemieID as integer
Using reader As XmlReader = XmlReader.Create(Application.StartupPath + "\data\enemies.xml")
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Name = "enemie" Then

                        
                        enemieID = reader("ID")
                       
                    End If
                End If
            End While
        End Using
;)

I LOVE SIMPLE CODE!

Thanks Kieken72, awesome!!!
Image
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: XML Help...PLEASE!!!
MrAksel
You could also save each XML node in the ListViewItem's tag property to improve performance
please lock this topic if its solved
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

Re: XML Help...PLEASE!!!
Kieken72
Scottie1972 wrote:
Kieken72 wrote:
Code: Select all
Dim enemieID as integer
Using reader As XmlReader = XmlReader.Create(Application.StartupPath + "\data\enemies.xml")
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Name = "enemie" Then

                        
                        enemieID = reader("ID")
                       
                    End If
                End If
            End While
        End Using
;)

I LOVE SIMPLE CODE!

Thanks Kieken72, awesome!!!
Enjoying you like it :)
7 posts Page 1 of 1
Return to “Coding Help & Support”