如何序列化模型,所以我可以使用NET的标准方法?
例如,我需要将数据序列化为XML,然后在没有Catel的完全不同的程序中使用它们。
我的模特课:
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));
}
在正常序列化中:
//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());
我明白了:
<?xml version="1.0" encoding="utf-8"?>
<MainModel graphid="1" xmlns:ctl="http://catel.codeplex.com">
...
然后我尝试:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.LoadXml(MySerializedToXMLModel);
我在LoadXML中遇到错误。