简体   繁体   中英

XML deserialisation fails with nested classes (inner classes)

Edit
I have a problem at de serialising an XML file, containing inner classes (or nested classes).

I have following class diagram:

    [XmlRoot(ElementName = "HM")]
    public class OwnClass : BaseClass{

    ...
    public OwnClass(){} // default constructor
    ...
    }

I have the following _xmlMessageFormatter declaration (based on System.Messaging ):

this._xmlMessageFormatter = new System.Messaging.XmlMessageFormatter();
System.Type[] OwnTypes = new System.Type[30];
OwnTypes[0] = typeof(Baseclasses.OwnClass);                   /* TR */
...
this._xmlMessageFormatter.TargetTypes = OwnTypes;

Edit: this is what the XML looks like:

<?xml version="1.0"?>
<HM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ID>124</ID>
    <TC>TR</TC>
</HM>

All of this is working fine.

Now I add a new class inside the definition of OwnClass :

    [XmlRoot(ElementName = "HM")]
    public class OwnClass : BaseClass {
        [XmlElement(ElementName = "INS")]
        public ClassInside f_Inside;

        ...
        public OwnClass(){} // default constructor
        ...
        public class ClassInside{
        ...
        public class ClassInside(){}
            ...} // end of ClassInside
        } // end of OwnClass

I've also added the corresponding targettype:

OwnTypes [27] = typeof(BaseClasses.OwnClass.ClassInside); // the number of the array is correct.

Edit: the XML file looks now as follows:

<?xml version="1.0"?>
<HM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ID>125</ID>
    <TC>TR</TC>
    <TR>
        <ID>1</ID>
        <CD>MOVE</CD>
    </TR>
</HM>

The _xmlMessageFormatter cannot handle the de serialisation, as you can see here:
Source code:

    Object temp;
    temp = this._xmlMessageFormatter.Read(message);

For getting more information, I've typed ? temp = this._xmlMessageFormatter.Read(message); ? temp = this._xmlMessageFormatter.Read(message); in the immediate window (I'm working with Visual Studio), this is what I get:

'temp = this._xmlMessageFormatter.Read(message)' threw an exception of type 'System.InvalidOperationException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233079
    HelpLink: null
    InnerException: {"There was an error reflecting field 'f_Inside'."}
    Message: "There was an error reflecting type 'BaseClasses.AnotherClass'."
    Source: "System.Xml"
    StackTrace: "   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)\r\n   at 
System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)\r\n   at 
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)\r\n   at 
System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)\r\n   at 
System.Messaging.XmlMessageFormatter.CreateTargetSerializerTable()\r\n   at 
System.Messaging.XmlMessageFormatter.Read(Message message)"
    TargetSite: {System.Xml.Serialization.TypeMapping ImportTypeMapping(System.Xml.Serialization.TypeModel, 
System.String, 
ImportContext, 
System.String, 
System.Xml.Serialization.XmlAttributes, 
Boolean, 
Boolean, 
System.Xml.Serialization.RecursionLimiter)}

I have two issues with the error message:

  • It mentions f_Inside . This looks correct, but I have used f_Inside as a general fieldname for all my classes, and the reason I mention this:
  • It mentions AnotherClass while I have send a message of the form OwnClass .

=> I'm having serious doubts about the correctness of the error message. Is there anybody who knows what I can do now (or how the _xmlFormatter works?)

Edit: added background
All of this is part of a messaging service: one application is sending a message, the other one is receiving it (using the System.Messaging.MessageQueue objects). The serialisation/deserialisation is just a part of it.

Thanks in advance

The problem is solved and it had nothing to do with deserialisation of nested classes:

In one of my classes ( AgainAnotherClass ), I had following source code:

  [XmlElement(ElementName = "SA")]
  [XmlElement(ElementName = "SA")]
  public string SomeAttribute { get; set; }

(a typical case of Copy/Paste)
The fact that I had two lines with XmlElement caused the problem.

The exception looked as follows:

  InnerException: {"There was an error reflecting field 'f_Inside'."}
  Message: "There was an error reflecting type '<NameSpace>.AgainOtherClass'."

The InnerException made me believe that there was a problem with the nested class, while the Message spoke about a completely other class. I decided to follow the InnerException .
That was wrong! So, in case of C# exceptions where InnerException and Message contradict each other, first check the Message , then (maybe) the InnerException .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM