c# XML-Object对象 序列化-反序列化
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xaml;
using System.Xml.Serialization; namespace BONC_BASE_Bonc_UI_Util_Xml
{
public class XmlUtil
{
/// <summary>
/// to object from xml
/// </summary>
/// <param name="type"></param>
/// <param name="xml"></param>
/// <returns></returns>
public static object Deserialize(Type type, string xml)
{
xml = ReplaceToType(xml);
// Console.WriteLine(xml);
try
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer xmldes = new XmlSerializer(type);
return xmldes.Deserialize(sr);
}
}
catch (Exception e)
{
Console.WriteLine("xml2Obj exception"+e.Message);
return null;
}
} /// <summary>
/// to xml from object
/// </summary>
/// <param name="type"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static string Serializer(Type type, Object obj)
{
MemoryStream Stream = new MemoryStream(); XmlSerializer xml = new XmlSerializer(type);
try
{
xml.Serialize(Stream, obj);
}
catch (InvalidOperationException ioe)
{
Console.WriteLine(ioe);
}
Stream.Position = ;
StreamReader sr = new StreamReader(Stream);
string str = sr.ReadToEnd(); //str = RepalceToClass(str); return str;
} public static String RepalceToClass(String xml)
{
return xml.Replace("xsi:type", "class");
} public static string RepalceToString(String xml)
{
return xml.Replace("xsd:string", "string");
} private static String ReplaceToType(String xml)
{
return xml.Replace("<ReturnObject>",
"<ReturnObject xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">")
.Replace("class=", "xsi:type=");
} public static string GetXmlJoint(string className, string innerXml)
{
string xmlJointTitle = "<?xml version=\"1.0\"?>\n";
string xmlClassnameF =
"<" + className + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n";
string xmlClassnameE = "</" + className + ">"; string xmlJoint = xmlJointTitle + xmlClassnameF + innerXml + xmlClassnameE;
return xmlJoint;
} public static string ReplaceList(string xml, String clazzName)
{
return xml.Replace("List", "ArrayOf"+clazzName);
} }
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using BONC_BASE_Bonc_UI_Util_Xml;
using NUnit.Framework; namespace BDIC_BASE.Bonc.UI.Util.Xml
{
public class XMLTest
{
[Test]
public static void Test()
{
Name1 name = new Name1();
name.Id = ""; List<Name1> names = new List<Name1>();
names.Add(name);
names.Add(name); Person person = new Person();
person.Names = names;
person.Obj = names; // Serialize
string xml = XmlUtil.Serializer(typeof(Person), person);
MessageBox.Show(xml); Person p = (Person)XmlUtil.Deserialize(typeof(Person), xml);
MessageBox.Show(p.Names[].Id);
}
} public class Person
{
public List<Name1> Names { get; set; } [XmlElement(Namespace = "")]
public Object Obj = new Object();
} public class Name1
{
public String Id { get; set; }
}
}
c# XML-Object对象 序列化-反序列化的更多相关文章
- Java对象序列化/反序列化的注意事项(转)
Java对象序列化 对于一个存在Java虚拟机中的对象来说,其内部的状态只是保存在内存中.JVM退出之后,内存资源也就被释放,Java对象的内部状态也就丢失了.而在很多情况下,对象内部状态是需要被持久 ...
- Java对象序列化/反序列化的注意事项
Java对象序列化 对于一个存在Java虚拟机中的对象来说,其内部的状态只是保存在内存中.JVM退出之后,内存资源也就被释放,Java对象的内部状态也就丢失了.而在很多情况下,对象内部状态是需要被持久 ...
- springboot学习(三)——http序列化/反序列化之HttpMessageConverter
以下内容,如有问题,烦请指出,谢谢! 上一篇说掉了点内容,这里补上,那就是springmvc的http的序列化/反序列化,这里简单说下如何在springboot中使用这个功能. 使用过原生netty ...
- C# XML对象序列化、反序列化
XML 序列化:可以将对象序列化为XML文件,或者将XML文件反序列化为对象还有种方法使用LINQ TO XML或者反序列化的方法从XML中读取数据. 最简单的方法就是.net framework提供 ...
- C# XML对象序列化、反序列化 - PEPE YU
http://www.tuicool.com/articles/IjE7ban http://www.cnblogs.com/johnsmith/archive/2012/12/03/2799795. ...
- JAXB序列化对象与反序列化XML
1.什么是JAXB JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术. 该过程中,JAXB也提供 ...
- <经验杂谈>C#/.Net中xml的Serialization序列化与DeSerializetion反序列化
1.先讲概念:.Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象.Serializ ...
- C# XML序列化/反序列化参考
.NET提供了很不错的XML序列化/反序列化器,(它们所在的命名空间为System.Xml.Serialization)这是很方便的,下面对它的使用做一些总结,以供参考. 1,简单序列化 public ...
- Java对象序列化与反序列化
对象序列化的目标是将对象保存在磁盘中或者在网络中进行传输.实现的机制是允许将对象转为与平台无关的二进制流. java中对象的序列化机制是将允许对象转为字节序列.这些字节序列可以使Java对象脱离程序存 ...
随机推荐
- mysql 远程登陆不上
当使用 TCP/IP 连接 mysql 时, 出现 : Can't connect to MySQL server on 'xxx.xxx.xxx.xxx.'(111) 这个错误. 经过重复折腾: 确 ...
- 公司的mysql-installer-community-5.7.19.0安装注意
需要安装Microsoft Visual C++ 2013 Redistributable(x64) 和 Microsoft Visual C++ 2013 Redistributable(x86) ...
- gray-code——找规律
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- webstrom 应用 转(http://blog.csdn.net/zghekuiwu/article/details/54382145)
WebStorm 是 JetBrains 推出的一款商业的 JavaScript 开发工具 任何一个编辑器都需要保存(ctrl + s),这是所有win平台上编辑类软件的特点,但是webstorm编辑 ...
- SAS学习经验总结分享:篇一—数据的读取
第一篇:BASE SAS分为数据步的作用及生成数据集的方式 我是学经济相关专业毕业的,从事数据分析工作近一年,之前一直在用EXCEL,自认为EXCEL掌握的还不错. 今年5月份听说了SAS,便开始学习 ...
- 导出数据生成Excel(MVC)
/// <summary> /// 生成Excel /// </summary> /// <returns></returns> public File ...
- Scrapy安装向导
原文地址 https://doc.scrapy.org/en/latest/intro/install.html 安装Scrapy Scrapy运行在python2.7和python3.3或以上版本( ...
- Hibernate学习二----------hibernate简介
© 版权声明:本文为博主原创文章,转载请注明出处 1.hibernate.cfg.xml常用配置 - hibernate.show_sql:是否把Hibernate运行时的SQL语句输出到控制台,编码 ...
- 一文了解@Conditional注解说明和使用
@Conditional:Spring4.0 介绍了一个新的注解@Conditional,它的逻辑语义可以作为"If-then-else-"来对bean的注册起作用. @Con ...
- 分区容量大于16TB的格式化
File systems do have limits. Thats no surprise. ext3 had a limit at 16 TB file system size. If you n ...