- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在反序列化来自 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/
我目前正在对一个 mmorpg 的二进制网络协议(protocol)进行逆向工程。我正在用 java 实现该协议(protocol)。 对于每个数据包类型,我将创建一个表示二进制数据的类。 例如,聊天
我正在尝试围绕现有类编写半透明包装器,我希望它能够模仿其他类的序列化。 例如,给定以下类: class Foo { [JsonConverter(CustomConverter)] s
是否有使用 Jackson 序列化和反序列化枚举集的简单方法? private enum Type { YES, NO } @JacksonXmlProperty(localName = "t
我很想知道当我们反序列化一个对象时会发生什么。 例如,如果我的类对象由许多其他对象组成,对象创建过程如何在反序列化过程中发生 最佳答案 对象是用默认的初始化字段创建的,然后用从串行流中获取的属性值填充
我正在尝试序列化和反序列化(使用 QDataStream 但这与这里无关)一个 enum class变量: enum class Type : char { Trivial, Comp
我不确定这到底有什么问题...它不会为我编译,我将它从 c 翻译成 C++(或尝试)...是的,我是初学者。谢谢! #include #include using namespace std; i
我遇到的问题与此处描述的问题非常相似:Combining type and field serializers case class(id: Option[UUID], otherValue:Stri
我们知道base中的apply()可以对数组的边距应用一个函数,边距应该是行或列。我想将边距扩大到“对角线” 和“反对角线”。结构看起来像 diagApply <- function(x, FUN,
我找到了 JSON serialization and deserialization to objects in Flutter 的例子但是如何使用像这样的人员列表来做到这一点: [ {
我有一个相当大的terms聚合结果,这些结果被加载到下拉列表中以提供filter功能。 可以说,我的下拉列表中有4000多种动物。我的另一个下拉列表有4种动物颜色。 例, animal --> ["d
我需要将 C# (.NET Framework 4.5.2) 中的一个类与 XML 序列化(反序列化),该类具有 string 的字典属性。键和 string[]数组值。我正在使用 Serializa
[已解决]应用给定的解决方案,效果很好! 程序的目的:在用户打开和关闭程序时保存/重新加载以前的数据。 我曾经用一个对象(obj)成功(反)序列化,现在我有两个不同类的不同对象。 我试图通过查看其他帖
问题 假设我有一个代表某事或其他的枚举: public enum ResultState { Found, Deleted, NotFound } 在我的序列化 json 中,
是否有取消 JSON 字符串的功能?我猜它不会内置到 JQuery 中,但它可以通过编写一个操纵字符串的脚本来实现吗?我在下面遇到了这个问题。 我正在使用 NYTimes API,但它不支持 JSON
对于这个问题,假设当对象完全写入流并成功读出时,或者当对象部分写入流并且读回对象时发生异常时,序列化/反序列化是原子的。假设写操作可能无法成功完成,例如因为停电了。 在Serializable的描述中
有谁知道时序检查是否仍在检测虚拟环境?我尝试使用 rdtsc 指令来获取 cpu 周期并比较真实 linux 机器和在 virtualbox 上运行的 linux 之间的结果。但结果似乎不稳定。有时,
我正在对一个(外部给定的)XML 文件进行操作,该文件具有以下形式的元素 10 20 30 40 50 60 70 80 我知道如何将属性作为属性处理(通过使用 [XmlAttri
我有一个通用的序列化器和反序列化器,用于通过网络连接发送的消息: public static async Task SerializeObject(Object obj) {
我正在考虑将当前基于 WCF 的应用程序迁移到 protobuf-net.Grpc。这似乎是可行的,但是我无法在不包含所有具有 [ProtoInclude] 属性的派生类的情况下使(DTO 类)基类的
我正在尝试将一些数据保存到文件中,但文件保存到的目录不正确。 using (StreamWriter sw = new StreamWriter(dir + "\\temp" + x + ".txt"
我是一名优秀的程序员,十分优秀!