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对象脱离程序存 ...
随机推荐
- MapWindowPoints
中文名 MapWindowPoints Windows CE 1.0及以上版本 头文件 winuser.h 库文件 user32.lib MapWindowPoints函数把相对于一个窗口的坐标空间的 ...
- Batch update returned unexpected row count from update [0];
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested ...
- Socket概述及TCP/IP的C++实现
网络通信实际是应用进程之间的通信,而要完整的描述一个应用进程在网络中的位置必须用 IP+端口: Socket就是一种在网络中进行数据通信的一种抽象描述.它是一种协议,本地地址,本地端口的抽象. Soc ...
- dos下连接mysql,显示表结构
C:\Windows\system32>mysql -hlocalhoset -uroot -p Enter password: ***** mysql> use ssh Database ...
- 如何让DIV居中
总结:text-align:center;对三中浏览器而言,都具有文字/行内元素的嵌套式居中,或者说继承式的居中,只要外面的容器设置了这个属性,那么他内部的所有元素都具有这个属性(意思是,虽然这个属性 ...
- oled屏幕
oled作为一种新型的有机显示屏,越来越现实出其重要性,它不但超薄可弯折并且可视视角较宽.处在不论什么角度看屏幕都不会造成图像的失真. 且它有三中原色:绿.红,蓝. 我近期在学安在智能车上的oled ...
- 应用程序之Xib自定义Cell
效果展示 结构分析 代码实现 一.效果展示 二.结构分析 1⃣️首先我们让我们的控制器不再继承UIViewController,而是继承UITableViewController.这样就直接遵守了de ...
- 迁移EXT4
http://fanli7.net/a/JAVAbiancheng/ANT/20101003/43604.html 級別: 中級 Roderick W. Smith ,顧問和作家 2008 年6 月0 ...
- Xenomai for Debian Jessie
安装内核源码包 apt install linux-source-3.16 安装其他编译需要的工具: apt install build-essential libc-dev libc6-dev pk ...
- Getting Started with the G1 Garbage Collector(译)
原文链接:Getting Started with the G1 Garbage Collector 概述 目的 这篇教程包含了G1垃圾收集器使用和它如何与HotSpot JVM配合使用的基本知识.你 ...