Welcome to Bangladesh Microsoft Technology Community Sign in | Join | Help

Be careful using Member Name with suffix "Specified", it has special meaning to the XmlSerializer

Members with names ending on 'Specified' suffix have special meaning to the XmlSerializer: they control serialization of optional ValueType members and have to be of type System.Boolean.

Here is a test class to generate the exception where I explicitly declared the 'generating_bugSpecified' Member to integer.

[System.Xml.Serialization.XmlIncludeAttribute(typeof(TestBugg))]
public class TestBug
{

[System.Xml.Serialization.XmlAttributeAttribute()]
public System.Int32 generating_bug;
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public int generating_bugSpecified;

}

Try serializing this above class with XmlSerializer, ie.

TestBug bug = new TestBug();
bug.generating_bug = 10;
SerializeObjectToFile(bug,"c:\\temp\\msg.txt");

public void SerializeObjectToFile( object objectToSerialize, string path)
{

File.Delete(path);
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(objectToSerialize.GetType());
x.Serialize(fs, objectToSerialize);
fs.Close()

}

You will find the following exception raised.

System.InvalidOperationException: There was an error reflecting type 'WindowsApplication1.TestBug'. ---> System.InvalidOperationException: Member 'abug_genSpecified' of type System.Int32 cannot be serialized. Members with names ending on 'Specified' suffix have special meaning to the XmlSerializer: they control serialization of optional ValueType members and have to be of type System.Boolean.
at System.Xml.Serialization.FieldModel..ctor(MemberInfo memberInfo, Type fieldType, TypeDesc fieldTypeDesc)
at System.Xml.Serialization.StructModel.GetFieldModel(FieldInfo fieldInfo)
at System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo)
at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel)
at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)

Published Wednesday, July 25, 2007 10:29 PM by Shahed

Comments

No Comments

Anonymous comments are disabled