我有一个XML,我想根据我自己的类进行反序列化。它正确地反序列化,但是有些值变为null。它不会产生错误,我也不知道错误在哪里。
我尝试过更改类,序列化内存模型,然后检查输出,但没有一个符合我的要求。它需要遵循所提供的XML。
我的模特:
[XmlRoot(ElementName = "model", Namespace = "http://www.archimatetool.com/archimate")]
public class Model
{
[XmlElement(ElementName = "folder")]
public List<Folder> Folders { get; set; }
[XmlElement(ElementName = "purpose")]
public string Purpose { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "archimate", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Archimate { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
}我的XML
<?xml version="1.0" encoding="UTF-8"?>
<archimate:model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:archimate="http://www.archimatetool.com/archimate" name="Archisurance" id="11f5304f" version="3.1.1">
<folder name="Business" id="8c90fdfa" type="business">
<folder name="Actors" id="fa63373b">
<element xsi:type="archimate:BusinessInterface" id="1544" name="mail"/>
</folder>
</folder>
<purpose>An example of a fictional Insurance company.</purpose>
</archimate:model>这是反序列化后的结果。我不能张贴图片(由于名誉),所以我只是张贴一个链接。
我希望目的字段能说“虚构的保险公司的例子”,但它是空的。
发布于 2019-03-31 21:26:52
可以使用以下数据模型反序列化XML:
[XmlRoot(ElementName = "model", Namespace = "http://www.archimatetool.com/archimate")]
[XmlType(Namespace = "http://www.archimatetool.com/archimate")]
public class Model
{
[XmlElement(ElementName = "folder", Form = XmlSchemaForm.Unqualified)]
public List<Folder> Folders { get; set; }
[XmlElement(ElementName = "purpose", Form = XmlSchemaForm.Unqualified)]
public string Purpose { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
}
[XmlType(Namespace = "http://www.archimatetool.com/archimate")]
public class Folder
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlElement(ElementName = "folder", Form = XmlSchemaForm.Unqualified)]
public List<Folder> Folders { get; set; }
[XmlElement(ElementName = "element", Form = XmlSchemaForm.Unqualified)]
public List<Element> Element { get; set; }
}
[XmlType(Namespace = "http://www.archimatetool.com/archimate")]
[XmlInclude(typeof(BusinessInterface))]
public abstract class Element
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
[XmlType(TypeName = "BusinessInterface", Namespace = "http://www.archimatetool.com/archimate")]
public class BusinessInterface : Element
{
}备注:
<archimate:model>位于archimate:命名空间中,但其子元素不在任何名称空间中,因为archimate:名称空间不是默认名称空间。因此,有必要向XmlSerializer指出,这些子元素与其父元素位于不同的名称空间中。设置XmlElementAttribute.Form = XmlSchemaForm.Unqualified可以实现这一点。
(没有必要指定属性在默认名称空间中,因为除非另有规定,否则所有XML属性都被假定为不限定的。)xsi:type="archimate:BusinessInterface"属性的存在表明<element>属性是多态类型层次结构的一部分。xsi:type属性是允许元素显式断言其类型的标准w3c属性。实际上,支持此属性。需要存在一个与xsi:type相对应的子类型,并通过[XmlInclude] attribute.For详细信息声明(参见https://learn.microsoft.com/en-us/dotnet/standard/serialization/how-to-control-serialization-of-derived-classes )。
在这里,我任意选择了要包含在基类Element和派生类BusinessInterface中的属性。如果有一个更完整的XML示例,您可能需要调整这一选择。样品小提琴这里。
https://stackoverflow.com/questions/55445234
复制相似问题