XML serialization issue

  • Thread starter Thread starter Barkar SE
  • Start date Start date
B

Barkar SE

Guest
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="XML Schema">
<issueDate2>0001-01-01</issueDate2>
</Id>

Where issueDate is still missing, but exist issueDate2 uninitialized.

What can be the reason of it?

Continue reading...
 
Back
Top