[英]C# - How to Deserialize XML with a List in a List
Here is the XML I am receiving as a response from a SOAP call:这是我收到的 XML 作为 SOAP 调用的响应:
<?xml version='1.0' encoding='UTF-8'?>
<Usage_Data Public="0">
<Type_Data Primary="1">
<Type_Reference>
<ID type="WID">4fae289a7fe541b098ca9448e462ff6b</ID>
<ID type="Communication_Usage_Type_ID">BUSINESS</ID>
</Type_Reference>
</Type_Data>
<Use_For_Reference>
<ID type="WID">7a232f5736a840a393b8ab43df7becd5</ID>
<ID type="Communication_Usage_Behavior_ID">BILLING</ID>
</Use_For_Reference>
<Use_For_Reference>
<ID type="WID">b58a4a54e04c4e1f8fc32bfc3b1a77cf</ID>
<ID type="Communication_Usage_Behavior_ID">SHIPPING</ID>
</Use_For_Reference>
<Use_For_Reference>
<ID type="WID">8f470e4f6ffd49638c80ea6b5443bddb</ID>
<ID type="Communication_Usage_Behavior_ID">REMIT</ID>
</Use_For_Reference>
</Usage_Data>
I have used this to create a C# class so that I can deserialize the XML.我已经使用它来创建 C# class 以便我可以反序列化 XML。 The generated class is ugly and unfortunately it doesn't work.
生成的 class 很难看,不幸的是它不起作用。 I have tweaked it enough to get it somewhat working.
我已经对其进行了足够的调整以使其有点工作。 The big problem is with the "Use_For_Reference" elements.
最大的问题在于“Use_For_Reference”元素。 The "type" attribute in each of the "ID" elements is not being pulled into the deserialized classes.
每个“ID”元素中的“type”属性没有被拉入反序列化的类中。
[Serializable]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class Usage_Data
{
private Usage_DataType_Data type_DataField;
private List<Usage_DataUse_For_ReferenceID> use_For_ReferenceField;
private byte publicField;
public Usage_DataType_Data Type_Data
{
get => type_DataField;
set => type_DataField = value;
}
[XmlArrayItem("ID", typeof(Usage_DataUse_For_ReferenceID), IsNullable = false)]
public List<Usage_DataUse_For_ReferenceID> Use_For_Reference
{
get => use_For_ReferenceField;
set => use_For_ReferenceField = value;
}
[XmlAttribute]
public byte Public
{
get => publicField;
set => publicField = value;
}
}
[Serializable]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true)]
public class Usage_DataType_Data
{
private Usage_DataType_DataID[] type_ReferenceField;
private byte primaryField;
[XmlArrayItem("ID", IsNullable = false)]
public Usage_DataType_DataID[] Type_Reference
{
get => type_ReferenceField;
set => type_ReferenceField = value;
}
[XmlAttribute]
public byte Primary
{
get => primaryField;
set => primaryField = value;
}
}
[Serializable]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true)]
public class Usage_DataType_DataID
{
private string typeField;
private string valueField;
[XmlAttribute]
public string type
{
get => typeField;
set => typeField = value;
}
[XmlText]
public string Value
{
get => valueField;
set => valueField = value;
}
}
[Serializable]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true)]
public class Usage_DataUse_For_ReferenceID
{
private string typeField;
private string valueField;
[XmlAttribute]
public string type
{
get => typeField;
set => typeField = value;
}
[XmlText]
public string Value
{
get => valueField;
set => valueField = value;
}
}
Please help me figure out how to get the "type" property populated from the XML deserialization.请帮我弄清楚如何从 XML 反序列化中填充“类型”属性。 Thanks!
谢谢!
Does this work for you?这对你有用吗?
// using System.Xml.Serialization;
// XmlSerializer serializer = new XmlSerializer(typeof(UsageData));
// using (StringReader reader = new StringReader(xml))
// {
// var test = (UsageData)serializer.Deserialize(reader);
// }
[XmlRoot(ElementName="ID")]
public class ID {
[XmlAttribute(AttributeName="type")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="Type_Reference")]
public class TypeReference {
[XmlElement(ElementName="ID")]
public List<ID> ID { get; set; }
}
[XmlRoot(ElementName="Type_Data")]
public class TypeData {
[XmlElement(ElementName="Type_Reference")]
public TypeReference TypeReference { get; set; }
[XmlAttribute(AttributeName="Primary")]
public int Primary { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="Use_For_Reference")]
public class UseForReference {
[XmlElement(ElementName="ID")]
public List<ID> ID { get; set; }
}
[XmlRoot(ElementName="Usage_Data")]
public class UsageData {
[XmlElement(ElementName="Type_Data")]
public TypeData TypeData { get; set; }
[XmlElement(ElementName="Use_For_Reference")]
public List<UseForReference> UseForReference { get; set; }
[XmlAttribute(AttributeName="Public")]
public int Public { get; set; }
[XmlText]
public string Text { get; set; }
}
Using: https://json2csharp.com/xml-to-csharp使用: https://json2csharp.com/xml-to-csharp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.