首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VB中的XML序列化

VB中的XML序列化
EN

Stack Overflow用户
提问于 2012-06-09 04:00:52
回答 1查看 51关注 0票数 0

我有一个"Product“类。每个产品都有一个名称。它由一个“小部件”列表组成。每个Widget都有一个编号。每个Widget又由一个Widget列表组成(深度可变)。在最后有一个或多个“SocketID”,每个套接字都有一个Sockets和一个长度。换句话说,最后一个Widget不是由另一个Widget组成,而是由Sockets组成。我想要的XML输出类似于this

我已经尝试了所有我能想到的技巧来定义我的类产品来实现序列化(ArrayList,List(of Widget),XMLInclude等)来实现这个结果--没有成功。以前有没有人遇到过这个问题?

EN

回答 1

Stack Overflow用户

发布于 2012-06-09 04:07:04

您可以在此处列出类(对象)的属性及其值。所以在这一步之后,你只需要用你喜欢的格式将它们保存在一个简单的文本文件中。

代码语言:javascript
运行
复制
Imports System.Reflection

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lvwProperties As System.Windows.Forms.ListView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lvwProperties = New System.Windows.Forms.ListView
        Me.SuspendLayout()
        '
        'lvwProperties
        '
        Me.lvwProperties.Dock = System.Windows.Forms.DockStyle.Fill
        Me.lvwProperties.FullRowSelect = True
        Me.lvwProperties.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable
        Me.lvwProperties.Location = New System.Drawing.Point(0, 0)
        Me.lvwProperties.Name = "lvwProperties"
        Me.lvwProperties.Size = New System.Drawing.Size(292, 273)
        Me.lvwProperties.Sorting = System.Windows.Forms.SortOrder.Ascending
        Me.lvwProperties.TabIndex = 0
        Me.lvwProperties.View = System.Windows.Forms.View.Details
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.lvwProperties)
        Me.Name = "Form1"
        Me.Text = "Properties"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Make column headers.
        lvwProperties.Columns.Clear()
        lvwProperties.Columns.Add("Property", 10, HorizontalAlignment.Left)
        lvwProperties.Columns.Add("Type", 10, HorizontalAlignment.Left)
        lvwProperties.Columns.Add("Value", 10, HorizontalAlignment.Left)

        ' List the properties.
        ' Use the class you want to study instead of Form1.
        Dim property_value As Object
        Dim properties_info As PropertyInfo() = _
            GetType(Form1).GetProperties()
        lvwProperties.Items.Clear()
        For i As Integer = 0 To properties_info.Length - 1
            With properties_info(i)
                If .GetIndexParameters().Length = 0 Then
                    property_value = .GetValue(Me, Nothing)
                    If property_value Is Nothing Then
                        ListViewMakeRow(lvwProperties, _
                            .Name, _
                            .PropertyType.ToString, _
                            "<Nothing>")
                    Else
                        ListViewMakeRow(lvwProperties, _
                            .Name, _
                            .PropertyType.ToString, _
                            property_value.ToString)
                    End If
                Else
                    ListViewMakeRow(lvwProperties, _
                        .Name, _
                        .PropertyType.ToString, _
                        "<array>")
                End If
            End With
        Next i

        ' Size the columns to fit the data.
        lvwProperties.Columns(0).Width = -2
        lvwProperties.Columns(1).Width = -2
        lvwProperties.Columns(2).Width = -2

        ' Size the form.
        Dim new_wid As Integer = 30
        For i As Integer = 0 To lvwProperties.Columns.Count - 1
            new_wid += lvwProperties.Columns(i).Width
        Next i
        Me.Size = New Size(new_wid, Me.Size.Height)
    End Sub

    ' Make a ListView row.
    Private Sub ListViewMakeRow(ByVal lvw As ListView, ByVal item_title As String, ByVal ParamArray subitem_titles() As String)
        ' Make the item.
        Dim new_item As ListViewItem = lvw.Items.Add(item_title)

        ' Make the sub-items.
        For i As Integer = subitem_titles.GetLowerBound(0) To subitem_titles.GetUpperBound(0)
            new_item.SubItems.Add(subitem_titles(i))
        Next i
    End Sub
End Class
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10955359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档