Deserialize Xml with empty elements

前端 未结 2 1700
甜味超标
甜味超标 2020-12-06 07:34

Consider the following XML:


    2
    
  

I need to deserialize this xml to an ob

相关标签:
2条回答
  • 2020-12-06 07:54

    There's a difference between a missing element and a null element.

    A missing element, <a><b>2</b></a>. Here C would take whatever default value you specify, using the DefaultValue attribute, or null if there's no explicit default.

    A null element <a><b>2</b><c xs:Nil='true'/></a>. Here you will get null.

    When you do <a><b>2</b><c></c><a/> the xml serializer will try to parse string.Empty as an integer an will correctly fail.

    Since your provider is generating invalid xml you will need to do this, if using the XmlSerializer:

    [XmlRoot(ElementName = "a")]
    public class A
    {
        [XmlElement(ElementName = "b")]
        public int? B { get; set; }
    
        [XmlElement(ElementName = "c")]
        public string _c { get; set; }
    
        public int? C
        {
            get
            {
                int retval;
    
                return !string.IsNullOrWhiteSpace(_c) && int.TryParse(_c, out retval) ? (int?) retval : null;
            }
        }
    }
    

    or slightly better using the DataContractSerializer

    [DataContract(Name="a")]
    public class A1
    {
        [DataMember(Name = "b")]
        public int? B { get; set; }
    
        [DataMember(Name = "c")]
        private string _c { get; set; }
    
        public int? C
        {
            get
            {
                int retval;
    
                return !string.IsNullOrWhiteSpace(_c) && int.TryParse(_c, out retval) ? (int?)retval : null;
            }
        }
    }
    

    although the DataContractSerializer doesn't support attributes if that's a problem.

    0 讨论(0)
  • 2020-12-06 08:11

    To deserialize empty tags like 'c' in your example:

        <foo>
            <b>2</b>
            <c></c>
        </foo>
    

    I used this approach. First it removes the null or empty elements from the XML file using LINQ and then it deserialize the new document without the null or empty tags to the Foo class.

        public static Foo ReadXML(string file)
        {
                Foo foo = null;
                XDocument xdoc = XDocument.Load(file);
                xdoc.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();
    
                XmlSerializer xmlSer = new XmlSerializer(typeof(Foo));
                using (var reader = xdoc.Root.CreateReader())
                {
                    foo = (Foo)xmlSer.Deserialize(reader);
                    reader.Close();
                }
                if (foo == null)
                    foo = new Foo();
    
                return foo;
        }
    

    Which will give you default values on the missing properties.

        foo.b = 2;
        foo.c = 0; //for example, if it's an integer
    

    I joined information from this links:

    Remove empty XML tags

    Use XDocument as the source for XmlSerializer.Deserialize?

    0 讨论(0)
提交回复
热议问题