惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - wu.g.q

旧电脑适合的系统 安川机器人遇见的问题汇总 Estun机器人数据断电保持问题解决方案 安川机器人快捷键 安川机器人变量代替常量的转换关系 创建自己的代码仓库 vc6.0 txt文件资源转为xaml资源 查看Windows操作系统编码方式 wpf 元素设置焦点无效的问题 汽车知识总结 wpf - 设置滚动条拇指(Thumb)大小 C# 反序列化乱码 C# 确定文件编码格式的方法 xsd.exe语法示例 C# 获取XML文件内容的多种方式 VC6.0 dll debug C#与C++动态链接库DLL参数互传 C#调用C/C++动态库dll异常:对 PInvoke 函数调用导致堆栈不对称问题 C#动态调用C/C++的DLL
C# 反序列化报错 XML 文档(1, 2)中有错误:不应有 <xml xmlns=''>
wu.g.q · 2023-08-01 · via 博客园 - wu.g.q

1.XmlSerializer
症状

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

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

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

我的xml如下:

<xml>
<ToUserName><![CDATA[gh_1874139df55e]]></ToUserName>
<FromUserName><![CDATA[ov4latyc1pi0_Ics0uHY6QTLRDg8]]></FromUserName>
<CreateTime>1388056811</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[哈哈]]></Content>
<MsgId>5961658608245054071</MsgId>
</xml>


要反序列化的对象类型:

public class WXP_Message
{
public int MessageId { get; set; }

public string ToUserName { get; set; }

public string FromUserName { get; set; }

public DateTime CreateTime { get; set; }

public string MsgType { get; set; }

public string Event { get; set; }

public string Content { get; set; }

public string PicUrl { get; set; }

public string Format { get; set; }

public string Location_X { get; set; }

public string Location_Y { get; set; }

public string Scale { get; set; }

public string Label { get; set; }

public string Title { get; set; }

public string Description { get; set; }

public string Url { get; set; }

public int MsgId { get; set; }

public int MediaId { get; set; }

public int ThumbMediaId { get; set; }

public bool IsReplied { get; set; }

public string ReplyContent { get; set; }

public DateTime ReplyTime { get; set; }

public bool IsGiftVoucher { get; set; }

public int IntTime { get; set; }

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

解决

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

<WXP_Message>
<ToUserName><![CDATA[gh_1874139df55e]]></ToUserName>
<FromUserName><![CDATA[ov4latyc1pi0_Ics0uHY6QTLRDg8]]></FromUserName>
<CreateTime>1388056811</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[哈哈]]></Content>
<MsgId>5961658608245054071</MsgId>
</WXP_Message>


参考链接:

 http://www.hierror.com/csharp/201397505.shtml 
-----------------------------------
©著作权归作者所有:来自51CTO博客作者rainbow70626的原创作品,请联系作者获取转载授权,否则将追究法律责任
XML 文档(1, 2)中有错误:不应有 &lt;xml xmlns=''&gt;
https://blog.51cto.com/u_6725876/5170297

要检查 XmlSerializer mySerializer = new XmlSerializer(destType, new XmlRootAttribute("jdma_epx"));  XmlRootAttribute里面的类型与xml的根类型是否一致