最近在做HL7V3的對接,關於XML的序列化和反序列化遇到xsi:type的問題解決方法
實體類定義:
public class HL7V3_ProviderOrganization { public HL7V3_Name Name { get; set; } } public class Bomb : HL7V3_Name { } [XmlInclude(typeof(Bomb))] public class HL7V3_Name { [XmlElement] public HL7V3_Name_Item Item { get; set; } } public class HL7V3_Name_Item { [XmlElement] public HL7V3_Ele_Value Part { get; set; } } public class HL7V3_Ele_Value { [XmlAttribute] public string Value { get; set; } }
XML序列化:
var s = new HL7V3_ProviderOrganization() { Name = new Bomb() { Item = new HL7V3_Name_Item() { Part = new HL7V3_Ele_Value() { Value = "surprise motherfucker!" } } } }.ToXml();
XML生成:
具體可參考:
https://docs.microsoft.com/en-us/dotnet/standard/serialization/controlling-xml-serialization-using-attributes