2011-07-23 3 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のメソッドを直接使用するか、XmlDocumentまたはXElementなどの方法を使用してください。

Btw。 DataContractSerializerは、生成されたXMLを完全に制御することはできません。たとえば、XML属性はサポートされていません。属性を使用する場合はXmlSerializerを使用する必要があります(Pointsシリアライゼーションに必要な)XMLシリアル化を完全に制御するにはIXmlSerializableを実装する必要があります。

関連する問題