gpt4 book ai didi

c# - 在 C# 中使用属性反序列化 XML

转载 作者:数据小太阳 更新时间:2023-10-29 02:48:54 25 4
gpt4 key购买 nike

我在反序列化来自 API 调用的 XML 响应时遇到问题。我的“选项”对象的属性“描述”为空。

下面是 XML 的示例:

<vehicle found="1">
<description>VehicleDescText</description>
<buildDate>2000-11-20</buildDate>
<modelYear>2001</modelYear>
<optionList>
<option code="UH8">OptionDesc1</option>
<option code="UH8">OptionDesc2</option>
</optionList>
</vehicle>

这是 C# 类的示例:

[DataContract]
[XmlRoot("vehicle")]
public class Vehicle
{
[DataMember]
[XmlAttribute("found")]
public bool Found { get; set; }

[DataMember]
[XmlElement("description")]
public string Description { get; set; }

[DataMember]
[XmlElement("buildDate")]
public string BuildDate { get; set; }

[DataMember]
[XmlElement("modelYear")]
public string ModelYear { get; set; }

[DataMember]
[XmlElement("optionList")]
public List<Option> OptionList { get; set; }
}

public class Option
{
[DataMember]
[XmlAttribute("code")]
public string Code { get; set; }

[DataMember]
[XmlElement("option")]
public string Description { get; set; }
}

反序列化对象如下所示:

var xmlDeserializer = new RestSharp.Deserializers.XmlDeserializer();
results = xmlDeserializer.Deserialize<Vehicle>(response);

我哪里错了?由于我不想修改底层数据模型,我可以添加或修改哪些属性来解决问题?

最佳答案

您已使用 XML serializer attributes 标记您的类型和 data contract attributes ,但是您正在使用的反序列化器,RestSharp.Deserializers.XmlDeserializer , 不支持这些属性。

相反,如其 documentation 中所述, 它支持 [DeserializeAs]允许控制 XML 节点名称和元素与属性状态的属性。

但正如 @apocalypse's answer 中所述以及this older question文档中有一个关于将元素值反序列化为属性值的特殊情况:

If the XML returned is like this:

<Response>Hello world</Response>

There's no way to directly represent that in a C# class:

public class Response {
}

You need something to hold the value of the Response element. In this case, add a property called Value and it will be populated:

public class Response {
public string Value { get; set; }
}

This condition is checked for between searching for matching element names and matching attribute names.

即如果您将 Description 重命名为 Value,您将能够成功反序列化该 XML。 ( Sample fiddle #1 .)

但是,您似乎不想重命名您的 Description 属性。如果是这样,您可以将 [DeserializeAs(Name = "Value")] 应用于它,特殊情况将再次适用:

public class Option
{
public string Code { get; set; }

[DeserializeAs(Name = "Value")]
public string Description { get; set; }
}

样本 fiddle #2 .

最后,作为替代解决方案,您可以切换到 RestSharp.Deserializers.DotNetXmlDeserializer并使用常规的 XmlSerializer 属性,特别是 [XmlText] .因此你的代码将变成:

var xmlDeserializer = new RestSharp.Deserializers.DotNetXmlDeserializer();
var results = xmlDeserializer.Deserialize<Vehicle>(response);

你的类型看起来像:

[XmlRoot("vehicle")]
public class Vehicle
{
[XmlAttribute("found")]
public bool Found { get; set; }

[XmlElement("description")]
public string Description { get; set; }

[XmlElement("buildDate")]
public string BuildDate { get; set; }

[XmlElement("modelYear")]
public string ModelYear { get; set; }

[XmlArray("optionList")]
[XmlArrayItem("option")]
public List<Option> OptionList { get; set; }
}

public class Option
{
[XmlAttribute("code")]
public string Code { get; set; }

[XmlText]
public string Description { get; set; }
}

样本 fiddle #3 .

关于c# - 在 C# 中使用属性反序列化 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47023764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com