1

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

michale
  • 35
  • 3
  • 9
  • but how i can do that after deserilizing all the objects in UI, if selected object how can serilize? – michale Apr 12 '12 at 02:48
  • see my answer, instead of populating controls eg listview.Add(item) - you should have a business object that exposes eg a List then in your GUI the only code is listview.DataSource = businessObject.ListOfItems. In the businessObject you'd have a method called saveSerializedListOfItems() – Jeremy Thompson Apr 12 '12 at 02:53

1 Answers1

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

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • I have added bit of my exisiting code ,which is doiing copy paste operation in my UI – michale Apr 12 '12 at 03:03
  • hmm probably i will take some more time to update you about this status,but before that can you look in to my question again i have modified it – michale Apr 12 '12 at 03:10
  • @michale refresh this page, I updated my answer before your last comment. I can see the code you have, simply add 2 methods to TrevoListView. One that Saves the items and the other that reads the items in. Once you got that going do it for PropertyGrid - whether in the same two functions or two new ones is up to you. – Jeremy Thompson Apr 12 '12 at 03:14
  • You could save the XML files to the Application.StaupPath or in the Users\Documents folder. It might even be best to give the users a Save/Open Dialog so they can choose where they wish to save the files – Jeremy Thompson Apr 12 '12 at 22:12
  • now i really doubt, serialization is the best way to produce xml file or need to use any other methods. becasue what ever subset .xml is created saved using Savefiledialog ,so once it saved it should be loaded in to new treeview ,serialized xml can be loaded in to tree view?? – michale Apr 13 '12 at 01:08
  • If you wish to persist an objects data you can manually save all the data to a xml file but its much easier/better to serialize the object to an XML file. Serialized data can be loaded into ListView, PropertyGrid (or even a TreeView now) as per the links in my Answer. – Jeremy Thompson Apr 13 '12 at 04:16
  • let me do thorough research abou this,,i thought of to go with loading in to xmldocument or xmtextwriter or xmltransformer – michale Apr 13 '12 at 05:50