简体   繁体   中英

Xml Empty Tag Deserialization

Could you please help me to find the solution to deserialize xml file which contains an empty tag?

Example is here:

<Report>
    <ItemsCount></ItemsCount>
</Report>

And I want to deserialize it into object of class like:

public class Report{
    public int? ItemsCount { get;set;}
}

my xml schema which i'm using in deserialization is:

[XmlRoot]
public partial class Report
{
   private int? itemsCount;

   [XmlElement(IsNullable = true)]
   public int? ItemsCount {
      get
      {
           return itemsCount;
      }
      set
      {
           itemsCount = value;
      }
}

It works well if the ItemsCount tag is missing at all, but if it is exist and is empty at the same moment, in that case it throwing the exception regarding lines there this tag is located in xml.

I saw a lot of links here while trying to find the solution, but without success.

And also, i don't want to just ignore the tag for all the cases, i want to get a null value instead then it is empty.

XmlSerializer is trying to convert string.Empty value of tag to integer and failing. Change your property as below to convert data type to string:

[XmlElement]
public string ItemsCount {
      get
      {
           return itemsCount;
      }
      set
      {
           itemsCount = value;
      }

This will set property Itemscount to empty in the above case. For null value for the above property the xml should be as below:

<ItemsCount xs:Nil='true'/>

How about this approach?

Define the class as follows:

public class Report
{
    [XmlIgnore]
    public int? ItemsCount { get; set; }
}

Due to the XmlIgnore attribute, this tag will be treated as unknown.
When creating the serializer add the event handler:

var xs = new XmlSerializer(typeof(Report));
xs.UnknownElement += Xs_UnknownElement;

In the event handler interpret an empty string as null:

private void Xs_UnknownElement(object sender, XmlElementEventArgs e)
{
    var report = (Report)e.ObjectBeingDeserialized;

    if (e.Element.InnerText == string.Empty)
        report.ItemsCount = null;
    else
        report.ItemsCount = int.Parse(e.Element.InnerText);
}

Use the serializer as usual:

Report report;
using (var fs = new FileStream("test.xml", FileMode.Open))
{
    report = (Report)xs.Deserialize(fs);
}

To my understanding, the described behaviour is correct; if the tag ItemsCount is missing, its value is null ; if it is empty, its value cannot be converted from "" to a value of int? . That being said, it would be possible to implement some custom parsing into the accessors of ItemsCount , which would have to be of type string . However, this seems more like a workaround to me. If possible, the document should be changed to begin with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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