Serialize nullable type to optional non-nillable element

前端 未结 2 1673
时光说笑
时光说笑 2021-01-18 22:35

I have an xsd schema with an optional element (minOccurs=0, maxOccurs=1) of type int. The element is NOT defined as nillable. In the d

2条回答
  •  一生所求
    2021-01-18 22:58

    I also encountered with this problem some time ago. I solved it by introducing additional property that handles serialization logic.

    1. Firstly you mark your original property with [XmlIgnore] attribute to exlude it from serialization/deserialization.

      // Mark your original property from serialization/deserialization logic
      [XmlIgnore]
      public int? OriginalProperty { get; set; }
      
    2. Then add the additional property and mark it with [XmlElement("YourElementName")] attribute to handle serialization logic.

      // Move serialization logic to additional string property
      [XmlElement("OriginalPropertyXmlElement")]
      public string OriginalPropertyAsString
      {
          get
          {
              //...
          }
          set
          {
              //...
          }
      }
      
    3. On deserialization it will:

      set
      {
          // Check the field existence in xml
          if (string.IsNullOrEmpty(value))
          {
              // Set the original property to null
              this.OriginalProperty = default(int?);
          }
          else
          {
              // Get value from xml field
              this.OriginalProperty = int.Parse(value);
          }
      }
      
    4. On serialization:

      get
      {
          if (this.OriginalProperty.HasValue)
          {
              // Serialize it
              return this.OriginalProperty.ToString();
          }
          else
          {
              // Don't serialize it
              return null;
          }
      }
      
    5. The example could look like:

      [XmlRoot("scene")]
      public class Scene
      {
          [XmlIgnore]
          public int? ParentId { get; set; }
      
          [XmlElement("parent_id")]
          public string ParentIdAsString
          {
              get
              {
                  return this.ParentId.HasValue ? this.ParentId.ToString() : null;
              }
      
              set
              {
                  this.ParentId = !string.IsNullOrEmpty(value) ? int.Parse(value) : default(int?);
              }
          }
      }
      

    It's pretty simple and you don't have to write your own serializer nor hacking xsd nor other stuff.

提交回复
热议问题