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