如何反序列化.dat文件以在窗体上创建控件? [英] How can de-serialize a .dat file to create controls on a form?

查看:123
本文介绍了如何反序列化.dat文件以在窗体上创建控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法序列化了面板上的每个对象,并将其放入具有某些属性的.dat文件中(我将在以后做所有可能的属性)。该文件如下所示:

I have managed to serialize each object on a panel and put it into a .dat file with some properties (I'll do all of the possible properties at a later date). This file looks like this:

<DocumentElement>
 <Controls>
   <Name>Label2</Name>
   <Text>hyvgiyb</Text>
   <Width>74</Width>
   <Height>21</Height>
   <Top>173</Top>
   <Left>269</Left>
 </Controls>
 <Controls>
   <Name>Label1</Name>
   <Text>Picture</Text>
   <Width>74</Width>
   <Height>21</Height>
   <Top>114</Top>
   <Left>240</Left>
 </Controls>
</DocumentElement

那只是两个控件,只有几个属性。现在,我想将Xml转换为控件的属性,以便添加控件并只需执行以下操作:

That is just two controls with a only a few properties. I now want to turn the Xml into properties of a control so I can add a control and simply do something like this:

'code to add control
'control properties
 .Name = DataFromXml
 .Text = DataFromXml
 '...etc

不用担心添加控件的代码,因为我已经拥有了;我只需要获取每个控件的单独属性(每个控件的属性都在。之间保持不变。这需要以某种方式加以区分)。

Don't worry about the code to add a control as I already have that; I just need to get the individual properties of each control (Each control's properties being held between the . Which will somehow need to be told apart).

为了澄清一下,我我正在读取.dat文件并从其中写入文件,并在Visual Studio 2012中使用VB.NET。
非常感谢您的帮助,谢谢:)

Just to clarify, I am reading and writing to/from a .dat file and using VB.NET in Visual Studio 2012. Any help is much appreciated, thanks:)

推荐答案

您可能不想实际读取XML文件。序列化时,无论(list(of?),collection?)中的控件位于何处,XML都可以将其反序列化为相同的内容,这可能会使其变得更容易。例如,如果您序列化了包含这些属性的类的集合,则可以将其取回。

You may not want to actually read the XML file. Whatever the controls where in (list(of?), collection?) when it was serialized, the XML can deserialize to that same thing which might make it easier. For instance, if you serialized a collection of classes containing those properties, you could get that back.

否则,也许将XML读入数据表中(还有其他方式):

Otherwise, read the XML into a datatable maybe (there are other ways):

Dim ds As New DataSet()
Dim dt as New DataTable
' Create new FileStream with which to read the file
Dim fsReadXml As New System.IO.FileStream _
    (myXMLfile, System.IO.FileMode.Open)
Try
    ds.ReadXml(fsReadXml)

Catch ex As Exception
    MessageBox.Show(ex.ToString())
Finally
    fsReadXml.Close()
End Try
dt = ds.Tables(0)

然后循环遍历表,以将属性值获取到添加到父级控件数组的NEW控件。 dt.Rows 每个将包含1个控件的数据,这些列将保存每个属性。自创建文件以来,您就知道列名是什么,并可以按名称引用它们:

then loop thru the table to fetch the property values to NEW controls you add to the parent's controls array. The dt.Rows will each contain 1 control's data, the columns will hold each property. Since you created the file, you know what the column names are and can reference them by name:

For each dr as Datarow  In ds.Tables(0).Rows
    Dim lbl as New Label

    With lbl
     .Top = dr.Item("Top")
     ' etc etc etc
    End with

   ' add to parent
    pnlThing.Controls.Add(lbl)

 Next

使用XML源遇到的事情是,如果其中存在不同类型的控件,则需要将ctl.Type保存为很好,但是然后从名称中解析出哪种控件。

The thing you will run into with an XML source is that if there are different kinds of controls in there, you will need to save the ctl.Type as well, but then Parse out what kind of control it is from the name.

编辑:二进制序列化程序更容易:

A binary serializer is easier:

    For Each ctl As Control In pnl1.Controls
        Dim c As New Class1
        c.sz = ctl.Size
        c.pt = ctl.Location
        c.name = ctl.Name

        L.Add(c)
    Next

    ' Persist to file
    Dim stream As FileStream = File.Create("C:\Temp\bin.dat")
    Dim formatter As New BinaryFormatter()
    formatter.Serialize(stream, L)         ' NOTE serialize the LIST of Class1
    stream.Close()

反序列化时,您将拥有一个包含所有道具的新列表(Class1)可以引用组或您已经知道的东西(如SIZE)而不是宽度和高度。它显然不会处理 Controls 数组,因此需要收集所需的任何内容。

When you deserialize, you will have a new List(of Class1) with all the props that you can ref in "groups" or sets of stuff you already know Like SIZE instead of Width And Height. It wont handle a Controls array apparently hence the need to collect whatever you need.

这篇关于如何反序列化.dat文件以在窗体上创建控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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