C# 图形对象的序列化
我有一个复杂的形状。应用程序允许绘制此形状的任何计数。 然后我必须将该图片保存为XML文件。如何将它们保存在XML文件中? 创建了My.xml,但只有这样的信息C# 图形对象的序列化,c#,xml,xml-serialization,gdi+,C#,Xml,Xml Serialization,Gdi+,我有一个复杂的形状。应用程序允许绘制此形状的任何计数。 然后我必须将该图片保存为XML文件。如何将它们保存在XML文件中? 创建了My.xml,但只有这样的信息 <?xml version="1.0"?> complexShapes是complexShapes的数组,它们通过单击按钮创建和绘制。尝试以下操作: public class ComplexShape { int x; int y; int a; // large elipse width/2
<?xml version="1.0"?>
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");
}
}
公共类ComplexShape
{
int x;
int-y;
int a;//大省略宽度/2
int b;//大省略高度/2
Form1 fr;
浮动角;
[XmlAttribute()]
公共int X{get{return X;}set{X=value;}}
[XmlAttribute()]
公共int Y{get{return Y;}set{Y=value;}}
[XmlAttribute()]
公共int A{get{return A;}set{A=value;}}
[XmlAttribute()]
公共int B{get{return B;}set{B=value;}}
[XmlIgnore()]
public Form1表单{get{return fr;}}
[XmlAttribute()]
公共浮点角度{get{return Angle;}set{Angle=value;}}
}
公共类绘图
{
列表形状=新列表();
[XmlIgnore()]
公共列表形状{get{return Shapes;}}
[XmlArray(“形状”)]
公共复合体形状[]形状阵列
{
获取{return shapes.ToArray();}
设置{shapes=新列表(值);}
}
公共void保存(字符串fname)
{
XmlSerializer xml_serializer=新的XmlSerializer(类型(绘图));
使用(Stream fstream=newfilestream(fname,FileMode.Create,FileAccess.Write,FileShare.None))
{
序列化(fstream,this);
fstream.Close();
}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
图纸dwg=新图纸();
dwg.Shapes.Add(新的ComplexShape());
dwg.Shapes.Add(新的ComplexShape());
dwg.Shapes.Add(新的ComplexShape());
dwg.Shapes.Add(新的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>
您需要在complexShapes
上定义属性,Xml序列化程序才能工作。Xml序列化不会序列化类的私有成员。与二进制序列化不同。此外,[Serializable]属性仅对二进制序列化起作用。请记住,永远无法序列化表单类,这是在反序列化形状时必须处理的问题。它不完全属于那个类。
<?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>