如果遇到:   根级别上的数据无效。 行 1,位置 1   。;即无法反序列化(反序列失败),得到对象为null ,把 xml 文本 Trim一下。

xml=xml.Trim();

序列化完毕你可以看到尾部有填充的 \0 。。。 要Trim掉。  参考:https://www.cnblogs.com/XChWaad/p/3346875.html

你可以TRIM 前后观察下Length. 有不可见空格

xml.Length
328

xml=xml.Trim();

xml.Length
327

XML SAMPLE:

<xml>

<bank_type><![CDATA[CFT]]></bank_type>
<cash_fee><![CDATA[1]]></cash_fee>
<fee_type><![CDATA[CNY]]></fee_type>
<is_subscribe><![CDATA[Y]]></is_subscribe>

<nonce_str><![CDATA[74971f5846d34fe0a35b8f636413f0e4]]></nonce_str>

<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<sign><![CDATA[C46252FFBA5F10F39F7A040F3BC5D58D]]></sign>

<sub_is_subscribe><![CDATA[N]]></sub_is_subscribe>

<time_end><![CDATA[20190417113750]]></time_end>
<total_fee>1</total_fee>
<trade_type><![CDATA[JSAPI]]></trade_type>

</xml>

--

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml; namespace SixunWxPayApi
{
public class XmlSerializerUtil
{ public static T Deserialize<T>( string xml)
{
      xml = xml.Trim(); //避免有不可见空格字符,导致无法反序列化
      Type type=typeof(T);
try
{ using (StringReader sr = new StringReader(xml))
{
XmlSerializer xmldes = new XmlSerializer(type);
return (T)xmldes.Deserialize(sr);
}
}
catch (Exception e)
{
// System.Diagnostics.Debug.WriteLine("ERROR " + e.StackTrace);
return default(T);
}
} //where T : class
public static string XmlSerializer<T>(T serialObject)
{
XmlSerializer ser = new XmlSerializer(typeof(T));
System.IO.MemoryStream mem = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8); // 强制指定命名空间,覆盖默认的命名空间
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty); ser.Serialize(writer, serialObject, namespaces);
writer.Close();

        string rst= Encoding.UTF8.GetString(mem.ToArray());
        rst = rst.Trim();//避免有不可见空格字符


        return rst;

        }

    }
}

--

实体类要加声明:

[XmlRootAttribute("xml", Namespace = "", IsNullable = false)]

调用:

WxResultBaseModel wrbm = XmlSerializerUtil.Deserialize<WxResultBaseModel>(strreturn);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization; namespace SixunWxPayApi
{
/// <summary>
/// 微信返回数据基础model,XML根元素是"xml"
/// </summary>
[XmlRootAttribute("xml", Namespace = "", IsNullable = false)]
public class WxResultBaseModel
{
public string result_code { get; set; }
public string return_code { get; set; } public string sign { get; set; } public string mch_id { get; set; } public string sub_mch_id { get; set; } public string out_trade_no { get; set; } public string transaction_id { get; set; } public string trade_state { get; set; } public string total_fee { get; set; } }
}

--

//如果是数组,一定要指明XmlElement
[XmlElement("bankAccountVo")]

C# .NET XML 序列化为对象,反序列化的更多相关文章

  1. XML序列化成对象

    这个是和ALM上传测试结果结合使用的//把xml序列化成对象以及把对象序列化成xml using System; using System.Data; using System.Configurati ...

  2. Json序列化为对象方法

    /// <summary>/// json 序列化为对象/// </summary>/// <typeparam name="T">对象类型&l ...

  3. Android中序列化对象到XMl 和 XML反序列化为对象

    package com.example.xmloperation; import java.io.File; import java.io.FileOutputStream; import java. ...

  4. jackson使用问题:mapper.readValue()将JSON字符串转反序列化为对象失败或异常

    问题根源:转化目标实体类的属性要与被转JSON字符串总的字段 一 一对应!字符串里可以少字段,但绝对不能多字段. 先附上我这段出现了问题的源码: // 1.接收并转化相应的参数.需要在pom.xml中 ...

  5. C# 后台解析json,简单方法 字符串序列化为对象,取值

    如果后台是一个JSON的字符串格式如下: string str = "{\"Success\":true,\"Msg\":\"成功!\&qu ...

  6. java 使用xom对象数据序列化为xml、反序列化、Preferences相关操作小案例

    package org.rui.io.xml; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import ...

  7. 匿名对象序列化为XML

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  8. C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化

    这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间. 为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中的对 ...

  9. JSON和XML格式与对象的序列化及反序列化的辅助类

    下面的代码主要是把对象序列化为JSON格式或XML格式等 using System; using System.Collections.Generic; using System.Globalizat ...

随机推荐

  1. 用Spring Boot去创建web service

    1. 环境 JDK1.8 JavaSE1.8 web容器是 webSphere IDE是Eclipse 2. 创建一个空的 Maven Project 3. 打开pom.xml 配置相应的packag ...

  2. holer实现外网访问内网数据库

    外网访问本地数据库 本地安装了数据库,只能在局域网内访问,怎样从公网也能访问内网数据库? 本文将介绍使用holer实现的具体步骤. 1. 准备工作 1.1 安装并启动数据库 默认安装的数据库端口是33 ...

  3. [Leetcode]27. 移除元素

    题目描述: ++难度:简单++ 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 ...

  4. 封装Thread的两种方法 via C++ in Linux

    方法一: 代理线程函数(proxyThreadFunc)作为类的静态成员函数, 回调函数指针作为类的私有成员变量 方法二: 代理线程函数(proxyThreadFunc)作为全局函数,  回调函数指针 ...

  5. Ubuntu和Linux的区别

    大家听别人介绍自己的Ubuntu时,会听到“我的操作系统是Linux的”.其实,这样介绍是缺乏严谨性滴.我们只要知道两点,基本上就搞清楚Linux和Ubuntu的关系:    1. 严格来说,Linu ...

  6. svcutil生成List类型不转换成数组

    svcutil http://localhost:22180/Service1.svc /out:c:\service1.cs /config:c:\config.config /ct:System. ...

  7. winedt102安装

    http://www.xue51.com/soft/3171.html 安装是安装上了,还是用不了,提示系统找不到文件什么的.最后还是安装winedt7. 注意要配置,miktex,这个东西.wine ...

  8. ES6模板字符串之标签模板

    首先,模板字符串和标签模板是两个东西. 标签模板不是模板,而是函数调用的一种特殊形式.“标签”指的就是函数,紧跟在后面的模板字符串就是它的参数. 但是,如果模板字符串中有变量,就不再是简单的调用了,而 ...

  9. android ui更新

    UI只能在主线程中更新. Handler 首先在主线程中创建handler,这样handler是附件到主线程UI中. Handler normalHandler = new Handler() { @ ...

  10. HDU - 6080 :度度熊保护村庄 (凸包,floyd最小环)(VJ1900题达成)

    pro:二维平面上,给定N个村庄.M个士兵驻守,把村庄围住,现在我们想留下更多的士兵休息,使得剩下的士兵任然满足围住村庄.N,M<500: sol:即是要找一个最小的环,环把村庄围住. 由于是环 ...