C# .NET XML 序列化为对象,反序列化
如果遇到: 根级别上的数据无效。 行 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 序列化为对象,反序列化的更多相关文章
- XML序列化成对象
这个是和ALM上传测试结果结合使用的//把xml序列化成对象以及把对象序列化成xml using System; using System.Data; using System.Configurati ...
- Json序列化为对象方法
/// <summary>/// json 序列化为对象/// </summary>/// <typeparam name="T">对象类型&l ...
- Android中序列化对象到XMl 和 XML反序列化为对象
package com.example.xmloperation; import java.io.File; import java.io.FileOutputStream; import java. ...
- jackson使用问题:mapper.readValue()将JSON字符串转反序列化为对象失败或异常
问题根源:转化目标实体类的属性要与被转JSON字符串总的字段 一 一对应!字符串里可以少字段,但绝对不能多字段. 先附上我这段出现了问题的源码: // 1.接收并转化相应的参数.需要在pom.xml中 ...
- C# 后台解析json,简单方法 字符串序列化为对象,取值
如果后台是一个JSON的字符串格式如下: string str = "{\"Success\":true,\"Msg\":\"成功!\&qu ...
- java 使用xom对象数据序列化为xml、反序列化、Preferences相关操作小案例
package org.rui.io.xml; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import ...
- 匿名对象序列化为XML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化
这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间. 为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中的对 ...
- JSON和XML格式与对象的序列化及反序列化的辅助类
下面的代码主要是把对象序列化为JSON格式或XML格式等 using System; using System.Collections.Generic; using System.Globalizat ...
随机推荐
- 18-09-16如何从pychram的第三方包导入设计器
1 在pychrm 中的操作 2 找到pycharm 中找到对应的包 3 找到设计器中文件夹 后进行复制即可
- 剑指Offer 65. 矩阵中的路径 (回溯)
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...
- Python_02
Python 判断语句 if,while if ture: print(1) else: print(0) for循环和内嵌函数range() range(a,b,c) a:起始位置 b:终止 ...
- 路径定义前+r
定义文件路径时前面加个r 例如 firstfolder = r"C:\Users\1261\Desktop\" 不对其中的符号进行转义
- 常见的CSS
/***** Selector Hacks ******/ /* IE6 and below */ * html #uno { color: red } /* IE7 */ *:first-child ...
- 关于freemarker 空变量的接收以及类型转换 笔记
通常接收一个变量是${siOrganid},如果并没有这个变量,是这么处理${siOrganid!},如果这个变量是某个类属性,是这么处理${interfsrv.siOrganid!},如果这个类也是 ...
- es6学习笔记-Proxy、Reflect、Promise
Proxy Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改,所以属于一种“元编程”(meta programming),即对编程语言进行编程. Proxy 可以理解成,在目标对象之前 ...
- .Net中World转PDF
using System;using System.Collections.Generic;using System.Linq;using System.Web;using Aspose.Words; ...
- vscode C++开发环境配置教程(教你如何用vscode写C++)
用了一段时间的cb,Devc++,但一直感觉cb的高亮太差,而Devc++使用体验差(尤其是代码补全功能),换过vs2017,但是由于其太大了,卡顿十分明显,所以最终选择了vscode这款轻量级编译器 ...
- 如何查看jar包的版本号?(转)
转自 : http://www.cnblogs.com/wych/p/4072913.html jar包根目录里的META-INF目录下的MANIFEST.MF文件里一般有会记录版本信息,可以到这个文 ...