2011-07-23 95 views
0

我有從Canvas類繼承的序列化的問題。我正在尋找更長時間的解決方案。我試過XMLSerializer,XAMLWriter現在試圖使用DataContractSerializer。從Canvas類繼承的序列化類

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="">&lt;NewCanvas Width="120" Height="150" xmlns="clr-namespace:Serializacja;assembly=Serializacja" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /&gt;</BaseProperties> 
    <Name i:type="x:string" xmlns="">test</Name> 
    <Rectangle i:type="x:string" xmlns="">&lt;Rectangle Width="20" Height="30" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /&gt;</Rectangle> 
    <PolyLine i:type="x:string" xmlns="">&lt;Polyline Points="10,10 30,30 50,10" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /&gt;</PolyLine> 
</NewCanvas> 

我想是這樣的,如果沒有這些redudant命名空間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

你想做什麼?將某些數據保存到XML和序列化實體之間有很大的區別。

當您從存儲的數據反序列化對象時,序列化允許您存儲對象的狀態並創建具有相同狀態的實例。你所描述的不是序列化,因爲你將無法從這些數據中獲得實例。爲了獲得數據,你需要序列化父類(整個層次結構)的內容,並且它們都不能被DataContractSerializer(也可能沒有序列化程序)序列化。

所以,如果你只是想保存一些數據,直接使用XmlWriter的方法或任何其他方法,如XmlDocumentXElement

Btw。 DataContractSerializer不提供對生成的XML的完全控制。例如,它不支持XML屬性。如果要使用屬性,則必須使用XmlSerializer,並且必須實現IXmlSerializable以完全控制XML序列化(這將在您的Points序列化中需要)。