嵌套类(内部类)的XML取消序列化失败



编辑
我在de序列化包含内部类(或嵌套类(的XML文件时遇到问题。

我有以下类图:

[XmlRoot(ElementName = "HM")]
public class OwnClass : BaseClass{
...
public OwnClass(){} // default constructor
...
}

我有以下_xmlMessageFormatter声明(基于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;

编辑:这就是XML的样子:

<?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>

所有这些都运行良好。

现在,我在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

我还添加了相应的目标类型:

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

编辑:XML文件现在如下所示:

<?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>

_xmlMessageFormatter无法处理de串行化,如您所见:
源代码:

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

为了获得更多信息,我在即时窗口中键入了? temp = this._xmlMessageFormatter.Read(message);(我正在使用Visual Studio(,这就是我得到的:

'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)rn   at 
System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)rn   at 
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)rn   at 
System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)rn   at 
System.Messaging.XmlMessageFormatter.CreateTargetSerializerTable()rn   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)}

我对错误消息有两个问题:

  • 它提到f_Inside。这看起来是正确的,但我已经使用f_Inside作为我所有类的通用字段名,我提到这一点的原因是:
  • 它提到AnotherClass,而我已经发送了形式为OwnClass的消息

=>我严重怀疑错误信息的正确性。有人知道我现在能做什么吗(或者_xmlFormatter是如何工作的?(

编辑:添加了后台
所有这些都是消息服务的一部分:一个应用程序正在发送消息,另一个正在接收消息(使用System.Messaging.MessageQueue对象(。串行化/去串行化只是其中的一部分

提前感谢

问题已经解决,它与嵌套类的取消序列化无关:

在我的一个类(AgainAnotherClass(中,我有以下源代码:

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

(复制/粘贴的典型案例(
我有两行XmlElement导致了问题。

异常情况如下:

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

InnerException让我相信嵌套类有问题,而Message则谈到了一个完全不同的类。我决定遵循InnerException
那是错误的!因此,在InnerExceptionMessage相互矛盾的C#异常的情况下,首先检查Message,然后(可能(检查InnerException

最新更新