Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

XML Reading Question

I am using VB.NET 2005.
This is my XML:
<?xml version="1.0" encoding="utf-8" ?>
<applicationlist>
      <app>
            <name>AC</name>
            <srcdir>c:\AC</srcdir>
            <incsub>False</incsub>
            <destdir>c:\storage\AC</destdir>
            <filename>acfile</filename>
            <Days>1</Days>
      </app>
      <app>
            <name>BF2</name>
            <srcdir>c:\Program Files\EA\BF2</srcdir>
            <incsub>True</incsub>
            <destdir>c:\storage\bf2</destdir>
            <filename>bf2file</filename>
            <Days>5</Days>
      </app>
                <app>
                  ..... (Can have many more)
                </app>
</applicationlist>

First I want to read in each App Name into a dropdown list.
Second when they have the specific app selected in the drop down box, how do I read the other elementsw for the specific app?

Please provide exmaple code.

Thanks
0
sneeri_c
Asked:
sneeri_c
1 Solution
 
bruintjeCommented:
Hi sneeri_c,

assuming this a follow up on your earlier question i build a load function and had to rebuild the read and write xml functions to work with the new setup

you probably need to change th path in the xml file name i used to test

1. placed a listbox on a form, + 2 textboxes one for the name [which is child 0 in the app node] other for hte srcdir [which is child 1 in the app node]
added 2 buttons one for loading the list and one for saving changes to the items from the textboxes

2. this is the code for loading the list in the box on a button clik
--------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  ' loading the list from file
  ListBox1.DataSource = readXMLList("g:\test3.xml", "/applicationlist/app", 0)
End Sub

Private Function readXMLList(ByVal filePath As String, ByVal ListKey As String, ByVal ChildNode As Integer) As Object
    Dim xd As New Xml.XmlDocument()

    'load the xml file
    xd.Load(filePath)

    'query for a value
    Dim Nodes As Xml.XmlNodeList = xd.DocumentElement.SelectNodes( _
                              "/" & ListKey)

    'return the nodelist or nothing if it doesn't exist
    If Not Nodes Is Nothing Then
        Dim Node As Xml.XmlNode
        Dim arrList As New ArrayList
        ' if we have a list we loop through the childnodes
        ' indicated by the childnode key
        For Each Node In Nodes
          arrList.Add(Node.ChildNodes(ChildNode).InnerText)
        Next
        Return arrList
    Else
        Return ""
    End If
End Function
--------

3. this the new read xml function [needed on listbox click] which i provided for a sample load of 2 items
--------
Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
  TextBox1.Text = readXMLValue("g:\test3.xml", "/applicationlist/app[name=""" & ListBox1.Text & """]", 0)
  TextBox2.Text = readXMLValue("g:\test3.xml", "/applicationlist/app[name=""" & ListBox1.Text & """]", 1)
End Sub

Private Function readXMLValue(ByVal filePath As String, ByVal key As String, ByVal index As Integer) As String
    Dim xd As New Xml.XmlDocument()

    'load the xml file
    xd.Load(filePath)

    'query for a value
    Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode(key)

    'return the value or nothing if it doesn't exist
    If Not Node Is Nothing Then
        Return Node.ChildNodes(index).InnerText
    Else
        Return ""
    End If
End Function
--------

4. this the new save xml function [needed on save button click] which i provided for a sample save of 1 item
--------
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  writeXMLValue("g:\test3.xml", "/applicationlist/app[name=""" & ListBox1.Text & """]", TextBox1.Text, 0)
End Sub

Private Function writeXMLValue(ByVal filePath As String, ByVal key As String, ByVal NewValue As String, ByVal index As Integer) As Boolean
    Try
        Dim xd As New Xml.XmlDocument()

        'load the xml file
        xd.Load(filePath)

        'save value
        Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
                                       key), Xml.XmlElement)
        If Not Node Is Nothing Then
            'key found, set the value

            Node.ChildNodes(index).InnerText = NewValue

            xd.Save(filePath)
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        Return False
    End Try
End Function
--------

hope this helps a bit
bruintje
0
 
sneeri_cAuthor Commented:
Thanks this is what I needed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now