简体   繁体   English

在VB.net中循环XML

[英]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. 我想做的是在XML文件(先生,夫人,博士...)中循环使用一堆前缀,然后将它们每个放入一个列表框,以便用户可以单击任意一个他们需要。

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. 我有点喜欢最后一种方式,因为它似乎更具描述性,因此,如果可能的话,我希望能够编写这样的XML。 If not, then I'll go with whatever works. 如果没有,那么我将尽一切努力。

I was using both For Each...Loops and For i...Loops. 我同时使用了For Each ... Loops和For i ... Loops。 Something like starting at <Prefixes> then looking at the first node, getting it's text, then the second, and so on? 类似于从<Prefixes>开始,然后查看第一个节点,获取其文本,然后查看第二个节点,依此类推吗?

You could use the XmlSerializer (to create / read the xml file) 您可以使用XmlSerializer(创建/读取xml文件)

For this, you need to define the XmlRoot , and the nodes that should be used inside the Xml (public properties) 为此,您需要定义XmlRoot以及Xml内部应使用的节点(公共属性)

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) 然后,可以将List(Of Prefix)附加到ListBox的DataSource / ItemsSource上(取决于您是否需要wpf或winforms或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 保存/创建XML文件

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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