簡體   English   中英

如何反序列化數組vb.net

[英]How to deserialize Array 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>

但是我不知道如何反序列化它,所以我得到了輸入數組。 我過去兩天一直在搜索互聯網。

提前致謝。

編輯:

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

這是我的沙發上的爛攤子...

這應該給您一個良好的開端(在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM