我試圖序列類,定義如下:類序列化爲XML沒有一個類名
[DataContract]
public class Human
{
public Human() { }
public Human(int id, string Name, bool isEducated)
{
this.id = id;
this.Name = Name;
this.isEducated = isEducated;
}
[DataMember]
public int id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public bool isEducated { get; set; }
}
那麼它是正在連載:
Human h = new Human(id, Name, isEducated);
XmlRootAttribute root = new XmlRootAttribute();
root.ElementName = "Repository";
XmlSerializer xs = new XmlSerializer(typeof(Human), root);
FileStream fs = new FileStream(fname, FileMode.Open);
xs.Serialize(fs, h);
這是結果:
<Repository xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>1</id>
<Name>Vill</Name>
<isEducated>false</isEducated>
</Repository>
不是我所期待。類名稱只是省略。這裏有什麼問題?
嘗試沒有「根」的東西。 – 2011-08-16 07:16:47