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

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - 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的根类型是否一致