学习C# XmlSerializer 序列化反序列化XML
2012-04-01 15:26
155 查看
类、变量常用头:
[XmlRootAttribute]:对根节点的描述,在类声明中使用 如:下例的Html类
[XmlType]:对节点描述,在类声明中使用 如:下例的Head类
[XmlElement]:节点下内部节点描述,如果对数组标识,是对数组单元描述 如:下例的Html.body,Head.title,Head.metas和Head.scripts数组...
[XmlAttribute]:节点下内部属性描述 如:下例的Meat.httpequiv,Meat.content,Script.src,Script.type,...
[XmlArrayItem]:数组单元项描述 如:下例的Body.lis
[XmlArray]:数组描述 如:下例的Body.lis
[XmlIgnore]:使该项不序列化 如:下例的Meta.data
[XmlText]:做为节点的text文本输出 如:下例的Script.content,Li.Content...
例如:
类定义代码
序列化
serializerString值为:
[XmlRootAttribute]:对根节点的描述,在类声明中使用 如:下例的Html类
[XmlType]:对节点描述,在类声明中使用 如:下例的Head类
[XmlElement]:节点下内部节点描述,如果对数组标识,是对数组单元描述 如:下例的Html.body,Head.title,Head.metas和Head.scripts数组...
[XmlAttribute]:节点下内部属性描述 如:下例的Meat.httpequiv,Meat.content,Script.src,Script.type,...
[XmlArrayItem]:数组单元项描述 如:下例的Body.lis
[XmlArray]:数组描述 如:下例的Body.lis
[XmlIgnore]:使该项不序列化 如:下例的Meta.data
[XmlText]:做为节点的text文本输出 如:下例的Script.content,Li.Content...
例如:
类定义代码
using System; using System.Xml.Serialization; [XmlRootAttribute("html")] public class Html { public Head head { get; set; } [XmlElement("body")] public Body body { get; set; } } [XmlType("head")] public class Head { [XmlElement("title")] public string titile; [XmlElement("meta")] public Meta[] metas; [XmlElement("script")] public Script[] scripts; } /// <summary> /// http-equiv="Content-Type" content="text/html; charset=utf-8" /// </summary> public class Meta { [XmlAttribute("http-equiv")] public string httpEquiv; [XmlAttribute] public string content; [XmlIgnore] public string data; } /// <summary> /// script src="/script/common.js" type="text/javascript" /// </summary> public class Script { [XmlAttribute] public string src; [XmlAttribute] public string type; [XmlText] public string content; } public class Body { [XmlElement("table")] public List<Table> tables=new List<Table>(); [XmlArray("ui")] [XmlArrayItem("li")] public List<Li> Lis = new List<Li>(); } public class Li { [XmlText] public string content; } public class Table { [XmlAttribute] public string height; [XmlAttribute] public string width; [XmlText] public string content; }
序列化
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Html)); Html html = new Html(); Head head = new Head(); head.title = "这是一个示例"; Meta[] metaArray = new Meta[1]; metaArray[0] = new Meta() { httpEquiv = "Content-Type", content = "text/html; charset=utf-8", data="该数据不被序列化" }; Script[] scriptArray = new Script[2]; scriptArray[0] = new Script() { type = "text/javascript", src = "/script/jquery.js" }; scriptArray[1] = new Script() { type = "text/javascript", content = "var number=6; alert('这是一个示例number='+number);" }; head.metas = metaArray; head.scripts = scriptArray; Body body = new Body(); body.tables.Add(new Table() { height = "5", width = "4", content = "这是table1" }); body.tables.Add(new Table() { content = "这是table2" }); body.Lis.Add(new Li() { content = "li1" }); body.Lis.Add(new Li() { content = "li2" }); html.head = head; html.body = body; string serializerString = ""; using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { System.IO.TextWriter writer = new System.IO.StreamWriter(stream, Encoding.UTF8); System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces(); ns.Add("", "");//不输出xmlns serializer.Serialize(writer, html, ns); stream.Position = 0; byte[] buf = new byte[stream.Length]; stream.Read(buf, 0, buf.Length); serializerString= System.Text.Encoding.UTF8.GetString(buf); }
serializerString值为:
<?xml version="1.0" encoding="utf-8"?> <html> <head> <title>这是一个示例</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="/script/jquery.js" type="text/javascript" /> <script type="text/javascript">var number=6; alert('这是一个示例number='+number);</script> </head> <body> <table height="5" width="4">这是table1</table> <table>这是table2</table> <ui> <li>li1</li> <li>li2</li> </ui> </body> </html>
相关文章推荐
- XML序列化及反序列化
- Java Json/xml 序列化和反序列化工具:JsonTools 和 simpleframework【附带实例】
- XML的序列化和反序列化
- xml 序列化和反序列化
- XmlSerializer 对象的Xml序列化和反序列化
- C# Json库 和 xml 序列化反序列化 存在的问题
- C# 序列化反序列化XML的帮助类
- XML序列化和反序列化及实例
- XStream 通过对象流进行对象与xml的序列化和反序列化
- 对象,对象集合的简单Xml序列化与反序列化 【实用代码,含泛型方法】
- windows phone8.1:Xml,Json序列化和反序列化
- XML的序列化与反序列化--两种属性风格
- Java 序列化与反序列化 —— 序列化为XML
- c# XML和实体类之间相互转换(序列化和反序列化)
- 用反射实现泛型集合与XML的序列化反序列化
- c# xml序列化和反序列化
- XmlSerializer 对象的Xml序列化和反序列化
- 序列化,反序列化(xml)字符串
- Xml序列化和反序列化
- 把类序列化成XML以及反序列化代码参考