2013-06-30 52 views
2

我有一個複雜的形狀。應用程序允許繪製這些形狀的任何數量。 然後,我必須將該圖片保存爲XML文件。我如何將它們保存在XML文件中? 我的.xml被創建,但只有這樣的信息。圖形對象的序列化

<?xml version="1.0"?> 

[Serializable, XmlRoot(Namespace = "http://www.intertech.com")] 
public class ComplexShape 
{ 
    int x; 
    int y; 
    int a; // large elipse width/2 
    int b; // large elipse height/2 
    Form1 fr; 
    float angle; 
} 
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     using (SaveFileDialog saveDlg = new SaveFileDialog()) 
     { 
      // Configure the look and feel of the save dialog. 
      saveDlg.InitialDirectory = "."; 
      saveDlg.Filter = "XML Files|*.xml"; 
      saveDlg.RestoreDirectory = true; 
      saveDlg.FileName = "MyShapes"; 

      if (saveDlg.ShowDialog() == DialogResult.OK) 
      { 
       XmlSerializer xml_serializer = new XmlSerializer(typeof(ComplexShape)); 
       using (Stream fstream = new FileStream(saveDlg.FileName, FileMode.Create, FileAccess.Write, FileShare.None)) 
       { 
        xml_serializer.Serialize(fstream, complexShapes); 
        fstream.Close(); 
        MessageBox.Show("serialized"); 
       }     
      } 
     } 
    } 

complexShapes是ComplexShapes的陣列,它們創造,按鈕上單擊繪圖。

+0

你需要在'complexShapes'定義屬性的XML序列化工作。 – ja72

+0

XML序列化不會序列化類的* private *成員。不像二進制序列化。另外,[Serializable]屬性對於二進制序列化只起作用。請記住,您永遠無法獲得序列化的Form類,這是在反序列化一個形狀時必須處理的。它並不完全屬於該類。 –

回答

2

試試這個:

public class ComplexShape 
{ 
    int x; 
    int y; 
    int a; // large elipse width/2 
    int b; // large elipse height/2 
    Form1 fr; 
    float angle; 

    [XmlAttribute()] 
    public int X { get { return x; } set { x = value; } } 
    [XmlAttribute()] 
    public int Y { get { return y; } set { y = value; } } 
    [XmlAttribute()] 
    public int A { get { return a; } set { a = value; } } 
    [XmlAttribute()] 
    public int B { get { return b; } set { b = value; } } 
    [XmlIgnore()] 
    public Form1 Form { get { return fr; } } 
    [XmlAttribute()] 
    public float Angle { get { return angle; } set { angle = value; } } 

} 

public class Drawing 
{ 
    List<ComplexShape> shapes = new List<ComplexShape>(); 

    [XmlIgnore()] 
    public List<ComplexShape> Shapes { get { return shapes; } } 

    [XmlArray("Shapes")] 
    public ComplexShape[] ShapesArray 
    { 
     get { return shapes.ToArray(); } 
     set { shapes = new List<ComplexShape>(value); } 
    } 

    public void Save(string fname) 
    { 
     XmlSerializer xml_serializer = new XmlSerializer(typeof(Drawing)); 
     using (Stream fstream = new FileStream(fname, FileMode.Create, FileAccess.Write, FileShare.None)) 
     { 
      xml_serializer.Serialize(fstream, this); 
      fstream.Close(); 
     } 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Drawing dwg = new Drawing(); 
     dwg.Shapes.Add(new ComplexShape()); 
     dwg.Shapes.Add(new ComplexShape()); 
     dwg.Shapes.Add(new ComplexShape()); 
     dwg.Shapes.Add(new ComplexShape()); 

     dwg.Save("ComplexShape.xml"); 
    } 
} 

與輸出:

<?xml version="1.0"?> 
<Drawing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Shapes> 
    <ComplexShape X="0" Y="0" A="0" B="0" Angle="0" /> 
    <ComplexShape X="0" Y="0" A="0" B="0" Angle="0" /> 
    <ComplexShape X="0" Y="0" A="0" B="0" Angle="0" /> 
    <ComplexShape X="0" Y="0" A="0" B="0" Angle="0" /> 
    </Shapes> 
</Drawing>