Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to save objects(and also load saved objects) ranging from lists, strings, checkbox statuses, radiobutton statuses etc to an XML file but I'm totally new to XML and wondering how this can be achieved?
Posted
Comments
F-ES Sitecore 11-Nov-15 11:01am    
Google "serialize object to xml c#". If you get stuck and have a specific question then feel free to ask. If the objects are not ones you've created yourself you might need to implement your own serialisation.
Member 11971544 11-Nov-15 11:12am    
Most of the examples I see are about serializing a class object but I created list objects,radiobutton objects in my form and I want to store their items,bool value respectively in an xml file.
F-ES Sitecore 11-Nov-15 11:20am    
What's the issue then? Decide on an xml structure that will hold your data and use XmlDocument to build that xml depending on what the properties of the object you want to serialise are. If you want to get advanced you can use reflection to work out all public properties of a type and create the xml from that.
Matt T Heffron 11-Nov-15 13:32pm    
Or similar to F-ES Sitecore's suggestion, create a class to hold the information you want to serialize, when you get the command to Save the information, copy the values/attributes from the controls into an instance of your class and then serialize it. On the Load command, deserialize back to an instance of your class and repopulate the control values/attributes.
Ehsan Sajjad 11-Nov-15 13:19pm    
PLease add relevant code what you have tried and where you are stuck, your question is too broad

To expand on Solution #3 here: I'd like to strongly suggest:

1. you use WCF (DataContract, DataMember) to serialize and de-serialize your objects. It is full-featured, easy to use, and powerful.

The XML it produces, while "verbose," can be compressed ... in my experience ... down to at least half (often less than 25%) the size of the XML.

2. you realize that you will need to create Classes to serailze/de-serialize. One reason why is:

2.a. you cannot serialize WinForm Controls, like the CheckBox (long story there).

So, if I have five CheckBoxes on my Form, the Class will serialize might look like this:
C#
 // required
using System.Runtime.Serialization;
using System.Windows.Forms
using System.Collections.Generic;

[DataContract]  // using WCF
public class SerializeForm1
{
    public SerializeForm1()
    {
        NmToChkState = new Dictionary<string, CheckState>();
    };

    private Dictionary<string, CheckState> _nmToChkState;
    [DataMember]
    public Dictionary<string, CheckState> NmToChkState
    {
        set { _nmToChkState = value; }
        get { return _nmToChkState; }
    }
}
When I am ready to save the state of the Form, I do the right thing to insert KeyValuePairs of the name of the CheckBox (Key), and the CheckState (Value).

When I de-serialize this written-to-XML object, I then iterate over the Dictionary and do the right thing to restore the CheckState values of each CheckBox based on their Names.

There are lots of good articles, and tutorials here; start with the ones Sergey recommended in Solution #3.
 
Share this answer
 
Comments
Matt T Heffron 11-Nov-15 19:29pm    
Good solution, it looks a lot like my comment above ;-)
+5
BillWoodruff 11-Nov-15 19:40pm    
Indeed, what I posted does demonstrate what you express in your comment ... I wasn't aware of that at the time I posted ... lack of sleep, stress :) ... if you had posted that as a solution, with a little code, you'd have my #5 :)
Matt T Heffron 11-Nov-15 19:49pm    
Fair enough. You put in the effort appropriate to a Solution. I just gave a "suggestion".
Member 11971544 12-Nov-15 3:45am    
Thanks BillWoodruff and Matt, very helpful suggestions and solution
Using JSON or XmlWriter directly, as advised in Solutions 1 and 3, will lead to inadequately low-level, and, most likely, ad-hoc code. Too much to be acceptable. You should better use something which is agnostic to types and anything specific to your data and does everything automatically for you. This is called serialization: https://en.wikipedia.org/wiki/Serialization[^].

And best kind of serialization in .NET, the most robust and the easiest to use at the same time is Data Contract: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

Please see my past answers:
how to pass a datacontract from a C#/WCF code to Managed C++[^],
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^],
deserializing list<class object> cause the objects property lose their reference to the objects of the base class[^].

—SA
 
Share this answer
 
Comments
BillWoodruff 11-Nov-15 18:01pm    
"will lead to inadequately low-level, and, most likely, ad-hoc code" yep, and take a look at what WCF writes out (some very weird syntax with bizarre tags) which is, in my experience, often compressible down to less than 25% of the size of the XML.
Are you using WPF ? The you can use XamlWriter to write a XAML file.

https://msdn.microsoft.com/en-us/library/system.windows.markup.xamlwriter(v=vs.110).aspx[^]
 
Share this answer
 
v2
Comments
Member 11971544 11-Nov-15 11:13am    
Using Windows Forms
Odd Marthon 11-Nov-15 11:14am    
Thats old, you should be using XAML and WPF
BillWoodruff 11-Nov-15 17:41pm    
This is not a helpful comment.
you can use Json.net
see http://www.newtonsoft.com/json/help/html/convertingjsonandxml.htm[^] for converting from json to xml.
basically you convert your objects to json string then to xml u can then save the xml string to file
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900