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. Youtube深度学习推荐系统论文

    https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45530.pdf https://zh ...

  2. linux中ps命令的用法说明

      在linux中使用ps命令可以查看有哪些进程在运行和运行的状态.进程是否结束.进程有没有僵尸.哪些进程占用了过多的资源等等. ps命令最常用的是用于监控后台进程的工作情况. 名称:ps  www. ...

  3. Oracle中group by 的扩展函数rollup、cube、grouping sets

    Oracle的group by除了基本使用方法以外,还有3种扩展使用方法,各自是rollup.cube.grouping sets.分别介绍例如以下: 1.rollup 对数据库表emp.如果当中两个 ...

  4. 一款炫酷Loading动画--载入成功

    简单介绍 昨天在简书上看到一篇文章.介绍了一个载入动画的实现过程 一款Loading动画的实现思路(一) 仅仅可惜原动画是IOS上制作的.而看了一下.作者的实现思路比較复杂,于是趁着空暇写了一个And ...

  5. 应用程序之TableView简单总结1

    UITableView的代理方法 修改Cell的状态 UITableView常见方法 一.UITableView的代理方法 #pragma mark 每一行的高度 - (CGFloat)tableVi ...

  6. ios 视图的旋转及应用

    有时候,需要做出如下图所示的效果,这就需要用到视图的旋转了 1.首先将旋转的值由角度转换为弧度: #define degreesToRadinas(x) (M_PI * (x)/180.0) 注:M_ ...

  7. 双向数据绑定---AngularJS的基本原理学习

    Angular JS (Angular.JS) 是一组用来开发Web页面的框架.模板以及数据绑定和丰富UI组件.它支持整个开发进程,提供web应用的架构,无需进行手工DOM操作. AngularJS非 ...

  8. 去掉activity默认动画效果的方法

    非常多手机都会自带一些Activity切换动画,项目中假设我们须要禁用掉系统Activity切换的动画.能够使用例如以下方法: 一.重写Activity的Them中的windowAnimationSt ...

  9. Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!

    using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...

  10. 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。

    // ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...