XML 反序列化因嵌套类内部类而失败

如何解决XML 反序列化因嵌套类内部类而失败

编辑
我在序列化包含内部类(或嵌套类)的 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)\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,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.Xml.Serialization.XmlAttributes,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res