简体   繁体   中英

Looping XML in VB.net

What I would like to do is take a bunch of Prefixes for names that are in an XML file (Mr, Mrs, Dr....) loop through them, and put them each into a listbox, so the user can just click whichever they need.

There must be an easier way than this:

<ObitSettings>
  <Prefixes>
    Mr.
  </Prefixes>
  <Prefixes>
    Mrs.
  </Prefixes>
  <Prefixes>
    Rev.
  </Prefixes>
  <Prefixes>
    Fr.
  </Prefixes>
  <Prefixes>
......

I had it another way where each Prefix had its own node:

<Prefixes>
  <Mister>Mr.</Mister>
  <Missus>Mrs.</Missus>
  ...
</Prefixes>

But that way was giving me everything in one long string, resulting in only on item in the listbox.

I kind of liked that last way, as it seemed more descriptive, so if it is possible I would like to be able to write the XML like that. If not, then I'll go with whatever works.

I was using both For Each...Loops and For i...Loops. Something like starting at <Prefixes> then looking at the first node, getting it's text, then the second, and so on?

You could use the XmlSerializer (to create / read the xml file)

For this, you need to define the XmlRoot , and the nodes that should be used inside the Xml (public properties)

You could then define if they should be elements, text, attributes, arrays, ...

The List(Of Prefix) could then be attached on the the DataSource/ItemsSource of a ListBox (depending if you need wpf or winforms or asp.net)

<XmlRoot("OrbitSettings")>
Public Class OrbitSettings
    <XmlElement("Prefixes")>
    Public Property Prefixes As List(Of Prefix)

    Public Sub New()
    End Sub
End Class

Public Class Prefix
    <XmlAttribute()>
    Public Property Label As String
    <XmlText>
    Public Property Value As String
End Class

To save / create the XML file

Function GetOrbitSettings(myStream As Stream) As OrbitSettings
    Dim serializer As New XmlSerializer(GetType(OrbitSettings))
    Dim orbitSetting As OrbitSettings = Nothing

    Try
        orbitSetting = DirectCast(serializer.Deserialize(myStream), OrbitSettings)
    Catch ex As Exception
        System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace)
        orbitSetting = New OrbitSettings() With {.Prefixes = New List(Of Prefix)}
    End Try
    Return orbitSetting
End Function

Function GetOrbitSettings(filename As String) As OrbitSettings
    If File.Exists(filename) Then
        Dim settings As OrbitSettings = Nothing
        Using fileStream As New FileStream(filename, FileMode.Open, FileAccess.Read)
            settings = GetOrbitSettings(New FileStream(filename, FileMode.Open, FileAccess.Read))
        End Using
        If settings IsNot Nothing Then
            Return settings
        End If
    End If
    Return New OrbitSettings() With {.Prefixes = New List(Of Prefix)}
End Function

Function SaveOrbitSettings(myStream As Stream, settings As OrbitSettings) As Boolean
    Dim succeeded As Boolean = False

    Try
        Dim serializer As New XmlSerializer(GetType(OrbitSettings))
        serializer.Serialize(myStream, settings)
        succeeded = True
    Catch ex As Exception
        System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace)
    End Try
    Return succeeded
End Function

Function SaveOrbitSettings(filename As String, settings As OrbitSettings) As Boolean
    Dim succeeded As Boolean = True
    Try
        Using Str As Stream = New FileStream(filename, FileMode.Create, FileAccess.Write)
            succeeded = SaveOrbitSettings(Str, settings)
        End Using
    Catch ex As Exception
        System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace)
        succeeded = False
    End Try
    Return succeeded
End Function

And a small main program as an example

Sub Main()
    Dim settings As New OrbitSettings With {.Prefixes = New List(Of Prefix)()}
    Dim filename As String = Environment.CurrentDirectory + "\prefixes.xml"

    settings.Prefixes.Add(New Prefix With {.Label = "Mr", .Value = "Mister"})
    settings.Prefixes.Add(New Prefix With {.Label = "Ms", .Value = "Miss"})
    settings.Prefixes.Add(New Prefix With {.Label = "Mss", .Value = "Misses"})

    SaveOrbitSettings(filename, settings)

    settings = GetOrbitSettings(filename)

    For Each prf As Prefix In settings.Prefixes
        Console.WriteLine("Found {0}: {1}", prf.Label, prf.Value)
    Next

    Console.ReadLine()
End Sub

which would then give an output of

<?xml version="1.0"?>
<OrbitSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Prefixes Label="Mr">Mister</Prefixes>
  <Prefixes Label="Ms">Miss</Prefixes>
  <Prefixes Label="Mss">Misses</Prefixes>
</OrbitSettings>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM