• 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>

    参考链接:

  • 相关阅读:
    Visual Studio2017 无法添加引用的解决方法
    第13周学习进度
    mininet之miniedit可视化操作
    构建之法阅读笔记05
    软件工程课堂练习找水王续
    第12周学习进度
    VS2015做单元测试
    学习调用第三方的WebService服务
    软件工程课堂练习找水王
    第11周学习进度
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/4886570.html
Copyright © 2020-2023  润新知