c# - 图形对象的序列化

标签 c# xml xml-serialization gdi+

我的形状很复杂。应用程序允许绘制任意数量的这种形状。 然后我必须将该图片保存为 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 的数组,它们在单击按钮时创建和绘制。

最佳答案

试试这个:

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>

关于c# - 图形对象的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17390712/

相关文章:

c++ - 如何将二进制数据缓冲区传递给 xml?

java - 手动 XML 序列化 Java 对象

wcf - 为什么 JAXB 为我的 WCF 服务生成错误的 XML?

c# - 使用 .NET 控件的 WiX 安装程序在 32 版本上设置注册表项。 64位,WoW6432节点

c# - 从 .NET DLL 文件生成 PDB?

c# - 尝试进行串行端口通信,返回 0x102 作为返回码

PHP 脚本在 mysql 失败时继续

c# - 为什么在调用 JValue.ToString() 和 JValue.ToString(Formatting.None) 时会得到不同的结果?

java - JAXB - 如何在没有 header 的情况下编码(marshal) Java 对象

c# - 将数组反序列化为复杂对象