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对象 序列化-反序列化的更多相关文章

  1. Java对象序列化/反序列化的注意事项(转)

    Java对象序列化 对于一个存在Java虚拟机中的对象来说,其内部的状态只是保存在内存中.JVM退出之后,内存资源也就被释放,Java对象的内部状态也就丢失了.而在很多情况下,对象内部状态是需要被持久 ...

  2. Java对象序列化/反序列化的注意事项

    Java对象序列化 对于一个存在Java虚拟机中的对象来说,其内部的状态只是保存在内存中.JVM退出之后,内存资源也就被释放,Java对象的内部状态也就丢失了.而在很多情况下,对象内部状态是需要被持久 ...

  3. springboot学习(三)——http序列化/反序列化之HttpMessageConverter

    以下内容,如有问题,烦请指出,谢谢! 上一篇说掉了点内容,这里补上,那就是springmvc的http的序列化/反序列化,这里简单说下如何在springboot中使用这个功能. 使用过原生netty ...

  4. C# XML对象序列化、反序列化

    XML 序列化:可以将对象序列化为XML文件,或者将XML文件反序列化为对象还有种方法使用LINQ TO XML或者反序列化的方法从XML中读取数据. 最简单的方法就是.net framework提供 ...

  5. C# XML对象序列化、反序列化 - PEPE YU

    http://www.tuicool.com/articles/IjE7ban http://www.cnblogs.com/johnsmith/archive/2012/12/03/2799795. ...

  6. JAXB序列化对象与反序列化XML

    1.什么是JAXB JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术. 该过程中,JAXB也提供 ...

  7. <经验杂谈>C#/.Net中xml的Serialization序列化与DeSerializetion反序列化

    1.先讲概念:.Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象.Serializ ...

  8. C# XML序列化/反序列化参考

    .NET提供了很不错的XML序列化/反序列化器,(它们所在的命名空间为System.Xml.Serialization)这是很方便的,下面对它的使用做一些总结,以供参考. 1,简单序列化 public ...

  9. Java对象序列化与反序列化

    对象序列化的目标是将对象保存在磁盘中或者在网络中进行传输.实现的机制是允许将对象转为与平台无关的二进制流. java中对象的序列化机制是将允许对象转为字节序列.这些字节序列可以使Java对象脱离程序存 ...

随机推荐

  1. 2017.3.14 activiti实战--第二十章--REST服务

    学习资料:<Activiti实战> 第二十章 REST服务 20.1 通信协议概述 略. 20.2 REST API概述 资源分类 资源基础URI 说明 Deployments manag ...

  2. node在Fedora 22系统下开发环境搭建

    事实上,环境搭建在linux系统还是比較简单的,下载已经编译好的包,配置一下环境变量. 或者下载源代码,自己编译. 这里记录一下,主要是node版本号变化节奏很块的情况下.怎样配置一次环境变量就不要再 ...

  3. 19. Spring Boot 添加JSP支持【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52017140 这个部分比较复杂,所以单独创建一个工程来进行讲解: 大体步骤: (1)     ...

  4. Android世界第一个activity启动过程

    Android世界第一个activity启动过程 第一次使用Markdown,感觉不错. Android系统从按下开机键一直到launcher的出现,是一个如何的过程,中间都做出了什么操作呢.带着这些 ...

  5. struts2学习笔记2 -struts2的开发步骤和工作原理

    struts2的开发步骤: 1.先定义一个能发送请求的页面,可以是链接,也可以是表单(form) 2.开发action类,struts2对action并没有过多的要求,只要求: a 推荐实现actio ...

  6. 本地aar文件引用

    有时须要使用第三方的aar库.或是project源码越来越大.项目内分工须要或出于模块化考虑.须要引用aar文件. arr就像C/C++中的静态库. 怎样建一个aar.网上的文章非常多,这里不再重述. ...

  7. HDU 2255 奔小康赚大钱 KM裸题

    #include <stdio.h> #include <string.h> #define M 310 #define inf 0x3f3f3f3f int n,nx,ny; ...

  8. 高性能HTTP加速器Varnish安装与配置(包含常见错误)

    Varnish是一款高性能的开源HTTP加速器.挪威最大的在线报纸Verdens Gang使用3台Varnish取代了原来的12台Squid,性能竟然比曾经更好.Varnish 的作者Poul-Hen ...

  9. python tkinter module的用法

    tkinter windows下从python3.2版本之后是自动安装的. python3.3之后的引入方式: >>> import _tkinter>>> imp ...

  10. 用buildroot qemu 执行 Android 系统

    准备 qemu. 编译 arm 的执行环境 $ wget http://wiki.qemu-project.org/download/qemu-2.0.0.tar.bz2 $ tar xzvf qem ...