时添加属性我有这个类序列化到XML
public class Audit
{
public string name { get; set;}
public DateTime AuditDate { get; set;}
public long? DepartmentId {get; set;}
public string Department { get; set;}
public long? StateId { get; set;}
public string? State { get; set; }
public long? CountryId { get; set; }
public string Country { get; set; }
}
当我序列,它看起来像这样
<Audit>
<name>George</name>
<AuditDate>01/23/2013</AuditDate>
<DepartmentId>10</DepartmentId>
<Department>Lost and Found</Department>
<StateId>15</StateId>
<State>New Mexico</StateId>
<CountryId>34</CountryId>
<Country>USA</Country>
</Audit>
我加入这个类来尝试获得ID字段属性
public class ValueWithId
{
[XmlAttribute ("id")]
public long? Id { get; set; }
[XmlText] // Also tried with [XmlElement]
public string Description { get; set; }
}
将我的课程改写为此
[Serializable]
public class Audit
{
public string name { get; set;}
public DateTime AuditDate { get; set;}
public ValueWithId Department { get; set;}
public ValueWithId State { get; set; }
public ValueWithId Country { get; set; }
}
但我得到的错误“有反射式审计的错误”
我试图让下面的XML
<Audit>
<name>George</name>
<AuditDate>01/23/2013</AuditDate>
<Department id=10>Lost and Found</Department>
<State id=15>New Mexico</State>
<Country id=34>USA</Country>
</Audit>
感谢
我仍然得到错误'有反映类型审计的错误' – plh 2013-02-12 17:43:21
@plh你删除了可空吗??? – giammin 2013-02-12 17:59:36
谢谢。就是这样,我的ID是空的。 – plh 2013-02-12 19:12:47