How to serialize controls

独自空忆成欢 提交于 2019-12-02 06:39:19

问题


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:


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



来源:https://stackoverflow.com/questions/10116354/how-to-serialize-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!