如何反序列化阵列vb.net [英] How to deserialize Array vb.net

查看:217
本文介绍了如何反序列化阵列vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个IRC bot。我想有一个是从XML文件中读取信息的命令。我有一个数组以下类:

I am currently writing an IRC Bot. I want to have information commands that are read from an XML file. I have an array the following class:

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

我已经有code序列化此

I already have the code to serialize this

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文档:

into the following XML document:

<?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天的网络连接。

But I have no idea how to deserialize it so I get back the input array. I've been searching the internet for the last 2 days.

先谢谢了。

编辑:

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

这是残局我有SOFAR ...

This is the mess I have sofar...

推荐答案

这应该给你一个良好的开端(测试和VS 2013的工作 - 写没关系)。对于阅读,请参阅这篇文章:

This should give you a head start (tested and working in VS 2013 - writes okay). For reading, refer to this article:

  • HOW TO: Serialize and Deserialize XML in Visual Basic .NET

我得立即运行,如果你有任何关于本实施方案的任何问题或你不能让你的阅读类回,请让我知道在注释中。

I gotta run now, if you have any questions regarding this implementation or you cannot make it read your class back, please let me know in comments.

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

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

Output (very close to what you need, may need some additional tweaking):

<?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>

这篇关于如何反序列化阵列vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆