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

I have a little problem that is a little frustrating. Is it possible to set a default value when deserializing xml in C # (.NET 3.5)? Basically, I'm trying to deserialize some xml that is not under my control, and one element looks like this:

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

it might also look like this:

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

Now in my class, I have the following property, which should receive data:

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

This works fine for the first example xml element, but the second fails. I tried changing the type of the property to be int? but it doesn’t help. I will need to serialize it back to the same XML format at some point, but I'm trying to use the built-in serialization support without resorting to using my own.

- ?

+3
3

, XML xsi: type xsi: nil, .

, XSLT, :

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

:

<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>

XmlSerializer . XSLT , . "" XSLT- "type" "nil", .

, XML- , , XSLT , .

+3

XmlSerializer xsi: nil - , IXmlSerializable. .

+1

Source: https://habr.com/ru/post/1698159/


All Articles