I am using XmlSerializer with XmlAttributeOverrides. The
XmlAttributeOverrides are used for properties which hold element/s
derived from "AbstractMessage" type.
System.Xml.Serialization.XmlRootAttribute(Namespac e = "http://
www.TestNameSpace.org/Test-4-4", IsNullable = false)]
class Root
{
[System.Xml.Serialization.XmlElementAttribute(Type =
typeof(Abstract_message), ElementName = "Message", IsNullable =
false)]
public virtual Abstract_message Message
{
get { return this._messageField; }
set{ this._messageField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(Type =
typeof(BatchMesssage), ElementName = "Batch", IsNullable = false)]
public virtual Batch_tCollection Batch
{
get { return this._batchField; }
set{ this._batchField = value; }
}
}
[System.Xml.Serialization.XmlTypeAttribute(Namespac e = "http://
www.TestNameSpace.org/Test-4-4")]
public class BatchMesssage
{
///
[System.Xml.Serialization.XmlElementAttribute("Hdr" , Order =
0, IsNullable = false)]
public BatchHeader_t Hdr
{
get{ return this._hdrField; }
set { this._hdrField = value; }
}
[System.Xml.Serialization.XmlElementAttribute("Mess age", Order
= 1, IsNullable = false, Type = typeof(Abstract_message_t))]
public Abstract_message_tCollection Messages
{
get { return this._messageField; }
set { this._messageField = value; }
}
}
where type "Batch_tCollection" is a collection of "BatchMessage" type
items.
For type "Root" "BatchMessage" I provide XmlAttributeOverrides for
property "Message" & "Messages" respt. :
System.Xml.Serialization.XmlAttributeOverrides attribOverrides = new
System.Xml.Serialization.XmlAttributeOverrides();
System.Xml.Serialization.XmlAttributes msgAttribs = new
System.Xml.Serialization.XmlAttributes();
msgAttribs.XmlElements.Add(new
System.Xml.Serialization.XmlElementAttribute("Test MessageRoot",
typeof(TestMessage)));
attribOverrides.Add(typeof(Root), "Message", msgAttribs);
attribOverrides.Add(typeof(BatchMesssage), "Message", msgAttribs);
where type "TestMessage" is derived from "AbstractMessage" type.
When I try to instantiate the XmlSerializer with the above
XmlAttributeOverrides as parameter to the constructor it throws the
following exception:
Exception:
System.InvalidOperationException was caught
Message="There was an error reflecting type 'Root'."
Source="System.Xml"
StackTrace:
at
System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(TypeModel
model, String ns, ImportContext context, String dataType,
XmlAttributes a, Boolean repeats, Boolean openModel)
at
System.Xml.Serialization.XmlReflectionImporter.Imp ortElement(TypeModel
model, XmlRootAttribute root, String defaultNamespace)
at
System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(Type
type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type,
XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute
root, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer..ctor(Type type,
XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute
root, String defaultNamespace)
Inner Exception: There was an error reflecting property 'Batch'.
Inner Exception: There was an error reflecting type 'BatchMessage'.
Inner Exception: Inconsistent sequencing: if used on one of the
class's members, the 'Order' property is required on all particle-like
members, please explicitly set 'Order' using XmlElement, XmlAnyElement
or XmlArray custom attribute on class member 'Messages'.
In the above case if I remove the sequencing property ("Order") from
the XmlElementAttribute applied on BatchMessage member properties, the
thing seems to work correctly.
Am I using the XmlAttributeOverride in the right place? Any help on
the above will be appreciated.
Thanks in advance.