简体   繁体   English

在C#(.NET 3.5)中反序列化xml时是否可以设置默认值?

[英]Is it possible to set a default value when deserializing xml in C# (.NET 3.5)?

I've got a little problem that's slightly frustrating. 我有一个小问题,有点令人沮丧。 Is it possible to set a default value when deserializing xml in C# (.NET 3.5)? 在C#(.NET 3.5)中反序列化xml时是否可以设置默认值? Basically I'm trying to deserialize some xml that is not under my control and one element looks like this: 基本上我正在尝试反序列化一些不受我控制的xml,一个元素看起来像这样:

<assignee-id type="integer">38628</assignee-id>

it can also look like this: 它也可以是这样的:

<assignee-id type="integer" nil="true"></assignee-id>

Now, in my class I have the following property that should receive the data: 现在,在我的课程中,我有以下属性应该接收数据:

[XmlElementAttribute("assignee-id")]
public int AssigneeId { get; set; }

This works fine for the first xml element example, but the second fails. 这适用于第一个xml元素示例,但第二个失败。 I've tried changing the property type to be int? 我试过将属性类型更改为int? but this doesn't help. 但这没有用。 I'll need to serialize it back to that same xml format at some point too, but I'm trying to use the built in serialization support without having to resort to rolling my own. 我也需要在某些时候将它序列化为相同的xml格式,但我正在尝试使用内置的序列化支持而不必诉诸于我自己。

Does anyone have experience with this kind of problem? 有没有人有这种问题的经验?

It looks like your source XML is using xsi:type and xsi:nil, but not prefixing them with a namespace. 看起来你的源XML使用的是xsi:type和xsi:nil,但没有为它们添加命名空间。

What you could do is process these with XSLT to turn this: 您可以做的是使用XSLT处理这些以使其变为:

<assignees>
  <assignee>
    <assignee-id type="integer">123456</assignee-id>
  </assignee>
  <assignee>
    <assignee-id type="integer" nil="true"></assignee-id>
  </assignee>
</assignees>

into this: 进入这个:

<assignees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <assignee>
    <assignee-id xsi:type="integer">123456</assignee-id>
  </assignee>
  <assignee>
    <assignee-id xsi:type="integer" xsi:nil="true" />
  </assignee>
</assignees>

This would then be handled correctly by the XmlSerializer without needing any custom code. 然后,XmlSerializer可以正确处理,而无需任何自定义代码。 The XSLT for this is rather trivial, and a fun exercise. XSLT对此非常简单,也是一项有趣的练习。 Start with one of the many "copy" XSLT samples and simply add a template for the "type" and "nil" attributes to ouput a namespaced attribute. 从许多“复制”XSLT示例中的一个开始,只需添加“type”和“nil”属性的模板即可输出命名空间属性。

If you prefer you could load your XML document into memory and change the attributes but this is not a good idea as the XSLT engine is tuned for performance and can process quite large files without loading them entirely into memory. 如果您愿意,可以将XML文档加载到内存中并更改属性,但这不是一个好主意,因为XSLT引擎已针对性能进行了调整,并且可以处理非常大的文件而无需将它们完全加载到内存中。

您可能需要查看OnDeserializedAttributeOnSerializingAttributeOnSerializedAttributeOnDeserializingAttribute以向序列化过程添加自定义逻辑

XmlSerializer uses xsi:nil - so I expect you'd need to do custom IXmlSerializable serialization for this. XmlSerializer使用xsi:nil - 所以我希望你需要为此做自定义IXmlSerializable序列化。 Sorry. 抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM