博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XML 文档(1, 2)中有错误:不应有 <xml xmlns=''>
阅读量:4312 次
发布时间:2019-06-06

本文共 2013 字,大约阅读时间需要 6 分钟。

症状

用XmlSerializer进行xml反序列化的时候,程序报错:

不应有 <xml xmlns=''>。

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: 不应有 <xml xmlns=''>。

我的xml如下:

1 <xml><ToUserName><![CDATA[gh_1874139df55e]]></ToUserName>
2 <FromUserName><![CDATA[ov4latyc1pi0_Ics0uHY6QTLRDg8]]></FromUserName>
3 <CreateTime>1388056811</CreateTime>
4 <MsgType><![CDATA[text]]></MsgType>
5 <Content><![CDATA[哈哈]]></Content>
6 <MsgId>5961658608245054071</MsgId>
7 </xml>

要反序列化的对象类型:

 
01 public class WXP_Message
02     {
03         public int MessageId { getset; }
04  
05         public string ToUserName { getset; }
06  
07         public string FromUserName { getset; }
08  
09         public DateTime CreateTime { getset; }
10  
11         public string MsgType { getset; }
12  
13         public string Event { getset; }
14  
15         public string Content { getset; }
16  
17         public string PicUrl { getset; }
18  
19         public string Format { getset; }
20  
21         public string Location_X { getset; }
22  
23         public string Location_Y { getset; }
24  
25         public string Scale { getset; }
26  
27         public string Label { getset; }
28  
29         public string Title { getset; }
30  
31         public string Description { getset; }
32  
33         public string Url { getset; }
34  
35         public int MsgId { getset; }
36  
37         public int MediaId { getset; }
38  
39         public int ThumbMediaId { getset; }
40  
41         public bool IsReplied { getset; }
42  
43         public string ReplyContent { getset; }
44  
45         public DateTime ReplyTime { getset; }
46  
47         public bool IsGiftVoucher { getset; }
48  
49         public int IntTime { getset; }
50  
51     }

 

 
诊断

这个错误一般都是xml不能反序列化为目标对象类型造成的,我的这个原因是因为:xml的根节点(xml)和对象名(wxp_message)不一样导致的不能反序列化

 
解决

修改xml根节点和对象类名一样就可以了

1 <WXP_Message><ToUserName><![CDATA[gh_1874139df55e]]></ToUserName>
2 <FromUserName><![CDATA[ov4latyc1pi0_Ics0uHY6QTLRDg8]]></FromUserName>
3 <CreateTime>1388056811</CreateTime>
4 <MsgType><![CDATA[text]]></MsgType>
5 <Content><![CDATA[哈哈]]></Content>
6 <MsgId>5961658608245054071</MsgId>
7 </WXP_Message>

 

转载于:https://www.cnblogs.com/simadi/p/8514065.html

你可能感兴趣的文章
Quartz.Net进阶之七:QuartzNet其他的功能简述
查看>>
消息队列
查看>>
WPF进阶教程 - 使用Decorator自定义带三角形的边框
查看>>
SQLServer之FOREIGN KEY约束
查看>>
redis 系列2 知识点概述
查看>>
图像滤镜艺术---图像滤镜晕影调节算法研究
查看>>
Win8Metro(C#)数字图像处理--2.21二值图像腐蚀
查看>>
MVC5 + EF6 入门完整教程
查看>>
SQL Server如何在变长列上存储索引
查看>>
Replication的犄角旮旯(八)-- 订阅与发布异构的问题
查看>>
Sliverlight实例之 绘制扇形和环形图
查看>>
Visual Studio 2012使用水晶报表Crystal Report
查看>>
你不知道的 页面编码,浏览器选择编码,get,post各种乱码由来
查看>>
SQLSERVER PRINT语句的换行
查看>>
Windows 8.1 应用开发 – 触控操作
查看>>
PowerDesigner创建物理模型
查看>>
使用Git、Git GUI和TortoiseGit
查看>>
vue---canvas实现二维码和图片合成的海报
查看>>
检查项目里是否有IDFA的方法
查看>>
64位系统使用Access 数据库文件的彻底解决方法
查看>>