none
XML serialization issue RRS feed

  • Question

  • Hi every body.

    I have issue when one of the XML's attributes is missing after the serialization.

    I made .cs file based on XSD file via Visual Studio's console.

    It looks as: 

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlRootAttribute("product", Namespace = "", IsNullable = false)]
        public partial class Product
    {
        private Id[] idField;
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public Id[] id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        public partial class Id
        {
            public System.DateTime issueDateField;
            [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, DataType = "date")]
            public System.DateTime issueDate
            {
                get
                {
                    return this.issueDateField;
                }
                set
                {
                    this.issueDateField = value;
                }
            }
        }
    }

    1) If I will create a new class with the same structure - all will work as a clock.

    2) If add a new variable issueDate2, for example, that will work too.

    3) If after adding variable in point 2 change problem's variable type, for example, to string and try to serialize Id class directly,

    Id id = new Id() { issueDate = "hooray" };
                
    XmlSerializer formatter = new XmlSerializer(typeof(Id3));
    
    using (FileStream fs = new FileStream(@"somepath\somename.xml", FileMode.CreateNew))
    {
        formatter.Serialize(fs, id);
    }

    then result will look:

    <Id xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <issueDate2>0001-01-01</issueDate2>
    </Id>
     

    Where issueDate is still missing, but exist issueDate2 uninitialized.

    What can be the reason of it?


    • Edited by Barkar SE Wednesday, February 13, 2019 10:39 AM
    Wednesday, February 13, 2019 10:38 AM

Answers

  • My description of the issue doesn't correct. I cut part of the class variables in the example for convenience of reading and it was mistake - after problem parameter exist one additional "*Specified" and this is the answer at this issue.



    • Marked as answer by Barkar SE Wednesday, February 13, 2019 7:49 PM
    • Edited by Barkar SE Wednesday, February 13, 2019 7:58 PM
    Wednesday, February 13, 2019 7:49 PM

All replies

  • Why can you use a class/object that is using auto properties?

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    Tutorial....

    https://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part-1

    Wednesday, February 13, 2019 6:26 PM
  • Hi, I have tried auto properties - same story.

    It looks more like problem with some kind of a cache. Clear solution does not help.

    Because when I am changing parameter's name then it starts work.


    • Edited by Barkar SE Wednesday, February 13, 2019 6:48 PM
    Wednesday, February 13, 2019 6:46 PM
  • My description of the issue doesn't correct. I cut part of the class variables in the example for convenience of reading and it was mistake - after problem parameter exist one additional "*Specified" and this is the answer at this issue.



    • Marked as answer by Barkar SE Wednesday, February 13, 2019 7:49 PM
    • Edited by Barkar SE Wednesday, February 13, 2019 7:58 PM
    Wednesday, February 13, 2019 7:49 PM