XML 文档(1, 2)中有错误:不应有 <xml xmlns=''>

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>

参考链接:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值