.Net 4.5 Framework XmlSerializer breaking changes

September 10, 2012 at 11:30 AMalex

The following code demonstrates that 4.5 and 4.0 XmlSerializer behavior is different.

 

See bug on Microsoft's Connect site for details:

 

https://connect.microsoft.com/VisualStudio/feedback/details/761786/net-4-5-xmlserializer-produces-different-output-from-4-0#details

Update 1/29/2013: Microsoft fixed this bug.

 

 

 

public class Named
    {
        private string _name;

        [XmlAttribute]
        public virtual string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        [XmlIgnore]
        public virtual bool NameSpecified
        {
            get
            {
                return _name != null && _name != string.Empty; //persist only non-empty
            }
            set
            { }
        }
    }

    public class BaseClass : Named
    {
        private string _customXmlName;

        /// <summary>
        /// Gets or sets a custom name for this field in XML
        /// </summary>
        public virtual string CustomXmlName
        {
            get
            {
                if (_customXmlName == null)
                {
                    return string.Empty;
                }

                return _customXmlName;
            }
            set
            {
                if (value != _customXmlName)
                {
                    _customXmlName = value;
                }
            }
        }

        [XmlIgnore]
        public virtual bool CustomXmlNameSpecified
        {
            get
            {
                return _customXmlName != null && _customXmlName != string.Empty; //persist only non-empty
            }
            set { }
        }
    }

    public class TestClass : BaseClass
    {
        private string _name;

        [XmlAttribute]
        public override string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        [XmlIgnore]
        public override bool NameSpecified
        {
            get
            {
                return _name != null && _name != string.Empty; //persist only non-empty
            }
            set
            { }
        }

        [XmlAttribute]
        public bool IsAttr { get; set; }

        public override string CustomXmlName
        {
            get
            {
                if (IsAttr)
                {
                    return "@" + Name;
                }
                return string.Empty;
            }
            set { }
        }

        [XmlIgnore]
        public override bool CustomXmlNameSpecified
        {
            get
            {
                return IsAttr; //persist only attribute
            }
            set
            { }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var obj = new TestClass() { IsAttr = true, Name = "Field0" };
            var ser = new XmlSerializer(typeof(TestClass));

            StringBuilder sb = new StringBuilder();
            XmlSerializer xs = new XmlSerializer(obj.GetType());
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Encoding = Encoding.Unicode;
            xws.Indent = false;
            xws.OmitXmlDeclaration = false;
            xws.NewLineOnAttributes = false;
            using (XmlWriter writer = XmlWriter.Create(sb, xws))
            {
                xs.Serialize(writer, obj);
            }
            var ret = sb.ToString();
        }
    }

 

Posted in: Development

Tags:

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading