【发布时间】:2011-08-16 07:12:04
【问题描述】:
我尝试序列化一个类,定义如下:
[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>
不是我所期望的。只是省略了类名。这里有什么问题?
【问题讨论】:
标签: .net xml xml-serialization