Click here to Skip to main content
15,898,939 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to serialize a class to xml file.Since I'm new to wpf and mvvm I find lot of difficulties.So anyone can explain in detail with codes and filenames.


Thanks.
Posted
Updated 5-May-14 10:27am
v2

serialization is nothing with wpf or mvvm (I assume your question is about serialization in general). The following is not the only way, but if you want to serialize an object:

C#
// File: AClass.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [Serializable]
    public class AClass
    {
        public string AProperty { get; set; }

        public void SerializeMe(string intoFile)
        {
            // http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter
            using (var fs = new StreamWriter(intoFile))
            {
                // http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
                var ser = new XmlSerializer(typeof(AClass));
                ser.Serialize(fs, this);
            }
        }
        public static AClass Deserialize(string fromFile)
        {
            AClass instance = null;
            // http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamreader
            using (var fs = new StreamReader(fromFile))
            {
                var ser = new XmlSerializer(typeof(AClass));
                instance = (AClass)ser.Deserialize(fs);
            }
            return instance;
        }
    }
}


use it as:

C#
// File: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test("test.xml");
        }

        static void Test(string xmlFile)
        {
            AClass a = new AClass() { AProperty = "prop value" };
            a.SerializeMe(xmlFile);

            var b = AClass.Deserialize(xmlFile);
        }
    }
}



more example: http://msdn.microsoft.com/en-us/library/58a18dwa%28v=vs.110%29.aspx
 
Share this answer
 
v3
Comments
Vedat Ozan Oner 5-May-14 17:21pm    
I think someone doesn't like my solution because of 'separation of concerns' :) yes, in terms of 'separation of concerns', this example is not good. it would be better to implement serialization issues in another class and let that class handle all serialization concerns. but you can still see how to serialize/deserialize objects in that example :)
WPF_Mvvm 5-May-14 22:25pm    
Thanks for your solution Vedat. But the problem is I couldn't make it out properly.Any other Examples ?
Getting error at this line
instance = (AClass)ser.Deserialize(fromFile);
-Its not a proper method to overload
Vedat Ozan Oner 6-May-14 4:07am    
I have updated the solution and code samples. you can now compile and run it.
WPF_Mvvm 7-May-14 2:50am    
Thanks a lot Vedat
Its Object to XML serialization. Not Class to XML.

Have a look at below link for XML Serialization example.
http://support.microsoft.com/kb/815813

And then use XmlDocument to save your XML string to File, as below.
C#
string exaample = "<xml><test></test></xml>";
XmlDocument xdocument = new XmlDocument();
xdocument.LoadXml(exaample);
xdocument.Save("TestXMLFile.xml");
 
Share this answer
 
Forget about WPF and MVVM, they are totally irrelevant. Embrace the idea of separation of concerns. And just learn about serialization:
http://en.wikipedia.org/wiki/Serialization[^],
http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.110%29.aspx[^].

Perhaps the most advanced and easiest to use is the serialization based on Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

In my past answers, I tried to explain why is that so:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA
 
Share this answer
 

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