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. Python moni模拟鼠标事件

    7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 4 ...

  2. 启动mongodb报错问题

    [root@zk-datanode-02 mongodb]# bin/mongod -f config/mongo.cnf &[1] 30549[root@zk-datanode-02 mon ...

  3. 转:java的各个拓展类库的推荐方案

    from: 链接:https://www.zhihu.com/question/21142149/answer/109854408 Java是世界上最强大的编程语言之一,很多开发人员和大型企业都偏爱J ...

  4. Ubuntu系统经常使用操作指令说明

    使用U盘拷贝压缩文件 文件的压缩方法详见:3.6文件归档压缩及其释放 U盘直接插入机器USB接口.等待自己主动弹出窗体,在弹出窗体选择"文件->打开终端",打开的终端当前文件 ...

  5. POJ 1785 Binary Search Heap Construction (线段树)

    题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质.并且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧. .. # ...

  6. Oracle 使用TRUNCATE TABLE删除所有行

    若要删除表中的所有行,则 TRUNCATE TABLE 语句是一种快速.有效的方法.TRUNCATE TABLE 与不含 WHERE 子句的 DELETE 语句类似.但是,TRUNCATE TABLE ...

  7. Nginx配置SSL安全证书避免启动输入Enter PEM pass phrase

    之前两篇文章已经很好的介绍了Nginx配置SSL的一些情况,配置好的Nginx每次启动都要 输两遍PEM pass phrase,很是不爽,尤其是在服务器重启后,Nginx压根就无法自动启动,必须手动 ...

  8. 六种基本DCDC变换器拓扑结构

    1.SEPIC电路 2.

  9. HDU1845Jimmy’s Assignment(无向图,最大匹配)

    题意:就是求最大匹配 #include<cstdio> #include<iostream> #include<algorithm> #include<cma ...

  10. Django之sitemap

    ##########settings.py SITE_ID=1 # Application definition # taggit tag INSTALLED_APPS = [ 'myblog', ' ...