How to serialize controls

2019-03-01 19:01发布

We have an application which contains database as xml files. It has client server architecture. So here server will read data from xml file by using dataset and store it in xml schema. Then server will serialize the data and pass it to the UI(client).So UI data’s are displayed by using Treeview on leftside, listview on right top and propertygrid on right bottom.

Data’s in Ui are categorized in to classes and objects. So now we have an xml file machinesclass.xml and machineobjects.xml in our database. machinesclass.xml contains various classes like electronic class,computer classs,agriculturetool class etc and machineobjects.xml contains TV,pentium4 computer,Tractror etc. So now in UI if I select electronic node from treeview ,it will list TV,radio,telephone etc what ever objects it contains in right top part by using Listview and if I select object "TV" related properties of TV are shown in propertygrid on right bottom.

So now we have a task that if somebody wants to take back up of selected objects from UI in terms of xml file(.xml), from parent machinesclass.xml and machineobjects.xml

for example if somebody selects TV from UI listview and wants to take back up in terms of .xml file(tv.xml) so that after sometime he can import data , what logic we can implement here? Can I serialize listview and propertygrid, or is there any options to do that? This is a bit of code i am using for copy paste operation within UI

1条回答
Melony?
2楼-- · 2019-03-01 19:44

Can I serialize listview and propertygrid, or is there any options to do that?

Here is how to serialize a ListView:
http://www.codeproject.com/Articles/3335/Persist-ListView-settings-with-serialization

Here is how to serialize a PropertyGrid:
http://www.codeproject.com/Articles/27326/Load-and-Save-Data-Using-PropertyGrid

My advice is to do it properly, and I think the correct solution would be to serialize the business objects that the ListView and PropertyGrid are bound too. Separate the business logic from the GUI, then its really easy!

Edit (after OP edited question to show code):

To Save Data to the XML file:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BinFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream FS = new System.IO.FileStream("C:\\tv.xml", IO.FileMode.Create);
BinFormatter.Serialize(FS, new ArrayList(listview1.Items));
FS.Close();

To Read Data in from an XML file:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BinFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
string fname;
System.IO.FileStream FS = new System.IO.FileStream("C:\\tv.xml", IO.FileMode.Open);
listview1.Items.AddRange(BinFormatter.Deserialize(FS).ToArray(typeof(ListViewItem)));
FS.Close();

Here's how you do it with a PropertyGrid: PropertyGrid.Serialize

查看更多
登录 后发表回答