可能重复:
将 C#/.NET 中的位图序列化为 XML
我正在尝试使用 XmlSerializer 序列化 MyClass,但看起来 [XmlInclude(typeof(Bitmap))] 不起作用。
using System;
using System.Drawing;
using System.IO;
using System.Xml.Serialization;
class Program {
static void Main() {
var myClass = new MyClass {
Name = "foo",
MyImage = new Bitmap(@"e:\pumpkin.jpg")
};
var serializer = new XmlSerializer(typeof(MyClass));
var fileStream = File.OpenWrite(@"e:\test.xml");
serializer.Serialize(fileStream, myClass);
}
}
[Serializable]
[XmlInclude(typeof(Bitmap))]
public class MyClass {
public string Name { get; set; }
public Bitmap MyImage { get; set; }
}
这是生成的文件:
<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>foo</Name>
<MyImage>
<Palette />
</MyImage>
</MyClass>