C#对象序列化成XML,以及自定义标签名
C#对象序列化操作:
public class XMLHelper
{
/// <summary>
/// 对象序列化成 XML String
/// </summary>
public static string XmlSerialize<T>(T obj)
{
string xmlString = string.Empty;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
//using (TextWriter textWriter = new StreamWriter("D:\\xmlTest.xml"))
//{
// xmlSerializer.Serialize(textWriter, obj);
//}
using (MemoryStream ms = new MemoryStream())
{
xmlSerializer.Serialize(ms, obj);
xmlString = Encoding.UTF8.GetString(ms.ToArray());
}
return xmlString;
} /// <summary>
/// XML String 反序列化成对象
/// </summary>
public static T XmlDeserialize<T>(string xmlString)
{
T t = default(T);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
{
using (XmlReader xmlReader = XmlReader.Create(xmlStream))
{
Object obj = xmlSerializer.Deserialize(xmlReader);
t = (T)obj;
}
}
return t;
} }
示例:
[Serializable]
[XmlType(TypeName = "MySong")]
public class Song
{
[XmlElement("A")]
public string Artist;
[XmlElement("S")]
public string SongTitle;
} class XMLDemo
{
static void Main(string[] args)
{
List<Song> list = new List<Song>()
{
new Song(){ SongTitle="歌曲标题1",Artist="歌手1"},
new Song(){ SongTitle="歌曲标题2",Artist="歌手2"},
new Song(){ SongTitle="歌曲标题3",Artist="歌手3"}
};
string xmlString = XMLHelper.XmlSerialize(list);
/*<?xml version="1.0"?>
<ArrayOfSong xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Song>
<Artist>歌手1</Artist>
<SongTitle>歌曲标题1</SongTitle>
</Song>
<Song>
<Artist>歌手2</Artist>
<SongTitle>歌曲标题2</SongTitle>
</Song>
<Song>
<Artist>歌手3</Artist>
<SongTitle>歌曲标题3</SongTitle>
</Song>
</ArrayOfSong> // 自定义标签名
<?xml version="1.0"?>
<ArrayOfMySong xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MySong>
<A>歌手1</A>
<S>歌曲标题1</S>
</MySong>
<MySong>
<A>歌手2</A>
<S>歌曲标题2</S>
</MySong>
<MySong>
<A>歌手3</A>
<S>歌曲标题3</S>
</MySong>
</ArrayOfMySong>
*/
Console.WriteLine(xmlString);
}
}
C#对象序列化成XML,以及自定义标签名的更多相关文章
- XmlSerializer 对象序列化成XML 自定义编码格式(gb2312)
随着面向服务(SOA)的开发方式的兴起,客户端和服务端之间的消息传送,很多采用了XML的格式.但是大家在日常的开发中,应该会有这么种体验,就是组织xml格式的代码太繁琐,这篇随笔也是为了和大家分享下简 ...
- 将对象序列化成XML字符串
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 将Java对象序列化成JSON和XML格式
1.先定义一个Java对象Person: public class Person { String name; int age; int number; public String getName() ...
- C#将对象序列化成JSON字符串
C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...
- 匿名对象序列化为XML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程
JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...
- 对象序列化成Json字符串 及 反序列化成对象
一. public static string JsonSerializer<T>(T t) { DataContractJsonSerializer ...
- ObjC 利用反射和KVC实现嵌套对象序列化成JSON数据
原理: 0.创建一个新的可变字典:NSMutableDictionary 1.采用class_copyPropertyList函数遍历对象的属性 2.property_getName获取属性名,val ...
- .NET 序列化成XML, 并且格式化
现有Person类: [Serializable] public class Person { public string Name; public string Info; public Perso ...
随机推荐
- Tensorflow r1.8安装C++接口(兼容OpenCV3)
与之前一样,直接走medium的传送门:https://medium.com/@fanzongshaoxing/use-tensorflow-c-api-with-opencv3-bacb83ca56 ...
- CSS外边距属性,深入理解margin
margin See the Pen margin by wmui (@wmui) on CodePen. 该属性用于设置元素的外边距,外边距是透明的,默认值0.这是一个简写属性,属性值最多为4个,例 ...
- 【中间件安全】Jboss安全加固规范
1. 适用情况 适用于使用Jboss进行部署的Web网站. 适用版本:5.x版本的Jboss服务器 2. 技能要求 熟悉Jboss安装配置,能够Jboss进行部署,并能针对站点使用Jboss进行安全加 ...
- C++将时间格式转换成秒数
#include <stdio.h> #include <time.h> #include <string.h> #include <stdlib.h> ...
- monit官方摘录
Here are the legal global keywords: Keyword Function ----------------------------------------------- ...
- [转]GREP for Windows
http://www.interlog.com/~tcharron/grep.html A very flexible grep for windows GREP is a well known to ...
- Install Local SQL In Mac OS
extends:http://www.cnblogs.com/maxinliang/p/3583702.html 一.安装 到MySQL官网上http://dev.mysql.com/download ...
- JavaScript 事件绑定函数
function panTest(m_onClickFun) { var This = this; This.onClickFun = m_onClickFun; /* This.onClickFun ...
- Vue2.0 $set()的正确使用方式
https://blog.csdn.net/panyang01/article/details/76665448
- hibernate04--三种状态之间的转换
public class StudentTest { Session session=null; Transaction transaction=null; //在执行测试方法之前 先执行before ...