Kai
Kai

Reputation:

Save properties of control to xml

I'd like to save some / all properties of a control to xml. E.g. the column widths of listivew like

listView1.SaveToXml("listview.xml")

How's this possible?

Upvotes: 2

Views: 1392

Answers (2)

Michael Todd
Michael Todd

Reputation: 17061

Via the XMLSerializer class.

And here's a good example of how to do it.

And, since Control's are not serializable, here is a way around that issue.

EDIT: Here are two more examples of how to do XML Serialization:
http://www.dotnetjohn.com/articles.aspx?articleid=173
http://devhood.mit.edu/Tutorials/tutorial_details.aspx?tutorial_id=236

As for exact code to do what you're trying to do, I realize now that we're actually using BinaryFormatter which is a little different from the way XMLSerializer does things, so providing code would not be useful to you. However, the algorithm is essentially:

  1. Grab the required property information (height, width, etc.) from the control you want to save and store these data in a class you made for that purpose.
  2. Store the class by using the Serialize method in XMLSerializer.
  3. When you need to load that control again, use the Deserialize method in XMLSerializer to load the class you saved previously.
  4. Create a new instance of the control you're trying to "revive", then copy the appropriate properties from the class you created into the control.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161791

You can try to use Xml Serialization.

Upvotes: 0

Related Questions