我有从Canvas类继承的序列化问题。我正在寻找更长时间的解决方案。我尝试使用XMLSerializer,XAMLWriter,现在尝试使用DataContractSerializer。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Rectangle rectangle = new Rectangle();
rectangle.Width = 20;
rectangle.Height = 30;
Polyline polyLine = new Polyline();
PointCollection pointCollection = new PointCollection(){new Point(10,10), new Point(30,30), new Point(50,10)};
polyLine.Points = pointCollection;
NewCanvas newCanvas = new NewCanvas("test", rectangle, polyLine);
newCanvas.Width = 120;
newCanvas.Height = 150;
newCanvas.SaveToXML(@"Save.xml");
}
}
[Serializable]
public class NewCanvas : Canvas, ISerializable
{
private string _name;
private Rectangle _rectangle;
private Polyline _polyLine;
public NewCanvas(string name, Rectangle rectangle, Polyline polyLine)
{
_name = name;
_rectangle = rectangle;
_polyLine = polyLine;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("BaseProperties", XamlWriter.Save(this));
info.AddValue("Name", _name);
info.AddValue("Rectangle", XamlWriter.Save(_rectangle));
info.AddValue("PolyLine", XamlWriter.Save(_polyLine));
}
public NewCanvas(SerializationInfo info, StreamingContext context)
{
//Deserialization implement
}
public void SaveToXML(string fileName)
{
DataContractSerializer ser = new DataContractSerializer(typeof(NewCanvas));
XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
using (XmlWriter writer = XmlWriter.Create(fileName, settings))
{
ser.WriteObject(writer, this);
writer.Close();
}
}
}
以上代码生成:
<?xml version="1.0" encoding="utf-8"?>
<NewCanvas xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.datacontract.org/2004/07/Serializacja">
<BaseProperties i:type="x:string" xmlns=""><NewCanvas Width="120" Height="150" xmlns="clr-namespace:Serializacja;assembly=Serializacja" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /></BaseProperties>
<Name i:type="x:string" xmlns="">test</Name>
<Rectangle i:type="x:string" xmlns=""><Rectangle Width="20" Height="30" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /></Rectangle>
<PolyLine i:type="x:string" xmlns=""><Polyline Points="10,10 30,30 50,10" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /></PolyLine>
</NewCanvas>
我想得到这样的东西,没有这些减少名称空间xlmns=""
而且没有重复<Rectangle ...>Rectangle ...</Rectangle>
<?xml version="1.0" encoding="utf-8"?>
<NewCanvas>
<BaseProperties Width="120" Height="150"/>
<Name>test</Name>
<Rectangle Width="20" Height="30"/>
<Polyline Points="10,10 30,30 50,10"/>
</NewCanvas>
你有什么想法我能做到这一点吗?
答案 0 :(得分:0)
你想做什么?将一些数据保存到XML和序列化实体之间存在很大差异。
序列化允许您在从存储数据反序列化对象时存储对象的状态并创建具有相同状态的实例。您所描述的不是序列化,因为您将无法从该数据中获取实例。要获取数据,您还需要序列化父类(整个层次结构)的内容,并且DataContractSerializer
(可能没有序列化程序)都不能序列化它们。
因此,如果您只想保存一些数据,只需直接使用XmlWriter
方法或XmlDocument
或XElement
等其他方法。
顺便说一下。 DataContractSerializer
无法完全控制生成的XML。例如,它不支持XML属性。如果要使用属性,则必须使用XmlSerializer,并且必须实现IXmlSerializable
才能完全控制XML序列化(这是Points
序列化所需的。)