0
我試圖通過使用XmlSerializer的序列化MyClass的,不過貌似[XmlInclude(typeof運算(位圖))]不作品。
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>
你的問題在這裏回答 - http://stackoverflow.com/questions/1907077/serialize-a-bitmap-in-c-net-to-xml;默認序列化不起作用的原因是因爲Bitmap沒有默認的無參數構造函數。 – dash