1

How to serialize a model, so I can use the standard methods of NET?

For example, I need to serialize the data into XML and then use them in a completely different program without Catel.

My Model class:

public class MainModel : ModelBase
    {
        public MainModel()
        {
        }

        public MainLicenseModel(MainLic data)
        {
            if (data == null)
            {
                UserName = String.Empty;
                UserData = String.Empty;
            }
            else
            {
                UserName = data.UserName;
                UserData = data.UserData;
            }
        }

        public String UserName
        {
            get { return GetValue<String>(UserNameProperty); }
            set { SetValue(UserNameProperty, value); }
        }
        public static readonly PropertyData UserNameProperty = RegisterProperty(nameof(UserName), typeof (String));


        public String UserData
        {
            get { return GetValue<String>(UserDataProperty); }
            set { SetValue(UserDataProperty, value); }
        }
        public static readonly PropertyData UserDataProperty = RegisterProperty(nameof(UserData), typeof (String));
    }

In normal serialization:

//model as MainModel
var xmlSerializer = SerializationFactory.GetXmlSerializer();
    xmlSerializer.OptimalizationMode = XmlSerializerOptimalizationMode.PrettyXmlAgressive;
    Stream stream = new MemoryStream();
    xmlSerializer.Serialize(model, stream);
    string text = Encoding.UTF8.GetString((stream as MemoryStream).ToArray());

I get:

<?xml version="1.0" encoding="utf-8"?>
<MainModel graphid="1" xmlns:ctl="http://catel.codeplex.com">
...

And then I try to:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.LoadXml(MySerializedToXMLModel);

I got error in LoadXML.

streamdown
  • 390
  • 4
  • 17

0 Answers0