如何反序列化Array vb.net

时间:2014-04-22 16:53:49

标签: xml arrays vb.net

我目前正在写一个IRC Bot。我想要从XML文件中读取信息命令。我有一个数组如下:

Public Class IrcCommand
    Public IsEnabled As Boolean
    Public Command As String
    Public Userlevel As Integer
    Public Message As String

    Public Sub New(ByVal e As Boolean, ByVal c As String, ByVal u As Integer, ByVal m As String)
        Me.IsEnabled = e
        Me.Command = c
        Me.Userlevel = u
        Me.Message = m
    End Sub
End Class

我已经有了序列化这个

的代码
Dim commands(2) As Command
commands(0) = New Command(True, "!test1", 0, "Hello there.")
commands(1) = New Command(True, "!test2", 0, "Test2")
commands(2) = New Command(True, "!test3", 0, "Test3")

进入以下XML文档:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<commands>
  <command IsEnabled="True" Command="!test1" Userlevel="0">Hello there.</command>
  <command IsEnabled="True" Command="!test2" Userlevel="0">Test2</command>
  <command IsEnabled="True" Command="!test3" Userlevel="0">Test3</command>
</commands>

但是我不知道如何反序列化它以便我返回输入数组。 我过去2天一直在网上搜索。

提前致谢。

编辑:

Imports System.Xml

Module Module1

    Sub Main()
        Dim reader As XmlTextReader = New XmlTextReader("test.xml")
        Dim command As New Command(True, String.Empty, 0, String.Empty)
        Dim commands As New List(Of Command)

        Do While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    If reader.Name = "command" Then
                        command = New Command(True, String.Empty, 0, String.Empty)
                        While reader.MoveToNextAttribute()
                            If reader.Name = "IsEnabled" Then
                                command.IsEnabled = CBool(reader.Value)
                            ElseIf reader.Name = "Userlevel" Then
                                command.Userlevel = CInt(reader.Value)
                            ElseIf reader.Name = "Command" Then
                                command.Command = reader.Value
                            End If
                        End While
                    End If
                Case XmlNodeType.Text
                    command.Message = reader.Value
                    commands.Add(command)
            End Select
        Loop
        output(commands)
        Console.ReadLine()
    End Sub

    Sub output(c As List(Of Command))
        For Each command As Command In c
            Console.Write("<command")
            Console.Write(" IsEnabled=""" & CStr(command.IsEnabled) & """")
            Console.Write(" Command=""" & command.Command & """")
            Console.Write(" Userlevel=""" & CStr(command.Userlevel) & """>")
            Console.Write(command.Message)
            Console.WriteLine("</command>")
        Next
    End Sub
End Module

这是我已经熟悉的混乱......

1 个答案:

答案 0 :(得分:0)

这应该会给你一个良好的开端(在VS 2013中测试和工作 - 写得好)。有关阅读,请参阅以下文章:

我现在要跑了,如果你对这个实现有任何疑问,或者你不能让它读回你的课程,请在评论中告诉我。

Imports System.Xml.Serialization

Module Module1

  Sub Main()
    Dim commandList As New IrcCommandList
    commandList.Commands.Add(New IrcCommand(True, "!test1", 0, "Hello there."))
    commandList.Commands.Add(New IrcCommand(True, "!test2", 0, "Test2"))
    commandList.Commands.Add(New IrcCommand(True, "!test3", 0, "Test3"))

    Using objStreamWriter As New IO.StreamWriter("C:\1\commands.xml")
      Dim x As New XmlSerializer(GetType(IrcCommandList))
      x.Serialize(objStreamWriter, commandList)
    End Using
  End Sub

End Module

<Serializable>
Public Class IrcCommand
  <XmlAttribute>
  Public IsEnabled As Boolean
  <XmlAttribute>
  Public Command As String
  <XmlAttribute>
  Public Userlevel As Integer
  <XmlText>
  Public Message As String

  'default constructor is required for serializable classes
  Public Sub New()
  End Sub

  Public Sub New(ByVal e As Boolean, ByVal c As String, ByVal u As Integer, ByVal m As String)
    Me.IsEnabled = e
    Me.Command = c
    Me.Userlevel = u
    Me.Message = m
  End Sub
End Class

<Serializable>
Public Class IrcCommandList
  <XmlArray("commands"), XmlArrayItem("command", GetType(IrcCommand))>
  Public Property Commands As New List(Of IrcCommand)

  'default constructor is required for serializable classes
  Public Sub New()
  End Sub
End Class

输出(非常接近你需要的,可能需要一些额外的调整):

<?xml version="1.0" encoding="UTF-8"?>

<IrcCommandList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <commands>
    <command Userlevel="0" Command="!test1" IsEnabled="true">Hello there.</command>
    <command Userlevel="0" Command="!test2" IsEnabled="true">Test2</command>
    <command Userlevel="0" Command="!test3" IsEnabled="true">Test3</command>
  </commands>
</IrcCommandList>