类、变量常用头:

[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...

例如:

类定义代码

 1 using System;
2 using System.Xml.Serialization;
3
4 [XmlRootAttribute("html")]
5 public class Html
6 {
7 public Head head { get; set; }
8
9 [XmlElement("body")]
10 public Body body { get; set; }
11 }
12
13 [XmlType("head")]
14 public class Head
15 {
16 [XmlElement("title")]
17 public string titile;
18
19 [XmlElement("meta")]
20 public Meta[] metas;
21
22 [XmlElement("script")]
23 public Script[] scripts;
24 }
25
26 /// <summary>
27 /// http-equiv="Content-Type" content="text/html; charset=utf-8"
28 /// </summary>
29 public class Meta
30 {
31 [XmlAttribute("http-equiv")]
32 public string httpEquiv;
33
34 [XmlAttribute]
35 public string content;
36
37 [XmlIgnore]
38 public string data;
39 }
40
41 /// <summary>
42 /// script src="/script/common.js" type="text/javascript"
43 /// </summary>
44 public class Script
45 {
46 [XmlAttribute]
47 public string src;
48 [XmlAttribute]
49 public string type;
50
51 [XmlText]
52 public string content;
53 }
54
55 public class Body
56 {
57 [XmlElement("table")]
58 public List<Table> tables=new List<Table>();
59
60 [XmlArray("ui")]
61 [XmlArrayItem("li")]
62 public List<Li> Lis = new List<Li>();
63 }
64
65 public class Li
66 {
67 [XmlText]
68 public string content;
69 }
70
71 public class Table
72 {
73 [XmlAttribute]
74 public string height;
75 [XmlAttribute]
76 public string width;
77
78 [XmlText]
79 public string content;
80 }

序列化

 1  System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Html));
2 Html html = new Html();
3 Head head = new Head();
4 head.title = "这是一个示例";
5 Meta[] metaArray = new Meta[1];
6 metaArray[0] = new Meta() { httpEquiv = "Content-Type", content = "text/html; charset=utf-8", data="该数据不被序列化" };
7 Script[] scriptArray = new Script[2];
8 scriptArray[0] = new Script() { type = "text/javascript", src = "/script/jquery.js" };
9 scriptArray[1] = new Script() { type = "text/javascript", content = "var number=6; alert('这是一个示例number='+number);" };
10 head.metas = metaArray;
11 head.scripts = scriptArray;
12 Body body = new Body();
13 body.tables.Add(new Table() { height = "5", width = "4", content = "这是table1" });
14 body.tables.Add(new Table() { content = "这是table2" });
15 body.Lis.Add(new Li() { content = "li1" });
16 body.Lis.Add(new Li() { content = "li2" });
17 html.head = head;
18 html.body = body;
19 string serializerString = "";
20 using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
21 {
22 System.IO.TextWriter writer = new System.IO.StreamWriter(stream, Encoding.UTF8);
23 System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
24 ns.Add("", "");//不输出xmlns
25 serializer.Serialize(writer, html, ns);
26 stream.Position = 0;
27 byte[] buf = new byte[stream.Length];
28 stream.Read(buf, 0, buf.Length);
29 serializerString= System.Text.Encoding.UTF8.GetString(buf);
30 }

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>

学习C# XmlSerializer 序列化反序列化XML的更多相关文章

  1. C# 序列化反序列化XML的帮助类

    以下是一个包装的用于序列化反序列化XML和C# 对象的类.  public class XmlSerializeHelper<T>     {         #region Serial ...

  2. XmlSerializer 对象的Xml序列化和反序列化

    http://www.cnblogs.com/yukaizhao/archive/2011/07/22/xml-serialization.html 这篇随笔对应的.Net命名空间是System.Xm ...

  3. XmlSerializer 对象的Xml序列化和反序列化,XMLROOT别名设置

    这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间.   为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中 ...

  4. C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化

    这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间. 为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中的对 ...

  5. XML序列化反序列化—常用类

    public class XMLSerializer    {        #region (public) xml序列化        /// <summary>        /// ...

  6. C# XML序列化/反序列化参考

    .NET提供了很不错的XML序列化/反序列化器,(它们所在的命名空间为System.Xml.Serialization)这是很方便的,下面对它的使用做一些总结,以供参考. 1,简单序列化 public ...

  7. 在.net中序列化读写xml方法的总结--转载过来学习学习

    原文章地址:http://www.cnblogs.com/fish-li/archive/2013/05/05/3061816.html 首先做个大概的总结,XML包括的元素有XmlElement,X ...

  8. 对类参数的序列化和反序列化XML

    /// <summary> /// Xml序列化与反序列化 /// </summary> public class XmlUtil { #region 反序列化 /// < ...

  9. XML序列化反序列化

    using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; nam ...

随机推荐

  1. HTML中的IE条件注释

    IE条件注释是一种特殊的HTML注释,这种注释只有IE5.0及以上版本才能理解.比如普通的HTML注释是: <!--This is a comment--> 而只有IE可读的IE条件注释是 ...

  2. Sql分隔字符串方法--split

    SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --DEClARE @str varchar(500)='a,b2,v5,d3,ew,2,3,dd' ...

  3. Centos优化Hadoop

    导读 Hadoop是一个能够让用户轻松架构和使用的分布式计算平台,用户可以轻松地在Hadoop上开发和运行处理海量数据的应用程序,本节讲安装并且优化centos 6.7 系统下的Supper Hado ...

  4. Unity3d Static 静态批处理和动态批处理

    表示物体时静态的,多用于静止不动的物体,此外static有多种,有的用于烘焙,有的用于遮挡剔除 物理效果是rigidbody组件,和这个没关系,用transform.Translate 无法移动,因为 ...

  5. ruby : Exception Notification

    https://github.com/smartinez87/exception_notification#sections Add the following line to your applic ...

  6. SNMP协议

           SNMP(Simple Network Management Protocol,SNMP)简单网络管理协议,其定义了传送管理信息的协议消息格式及管理站和设备代理相互之间进行消息传送的规程 ...

  7. Backpack | & ||

    Backpack | Given n items with size Ai, an integer m denotes the size of a backpack. How full you can ...

  8. maven3 junit4 spring3 jdk8 :junit一直报错,害的我几个星期都是这个错,你妹的!

    [org.springframework.test.context.junit4.SpringJUnit4ClassRunner]SpringJUnit4ClassRunner constructor ...

  9. 当年的文曲星cc800

    你还记得当年的cc800吗?还记得黄金英雄传说吗?还记得用cc800编程的日子吗... 今天突然想起了我的cc800,好怀念那段爬在家里的阳台的木架子上,挠着头,编程序的日子...可惜,当时比较穷,没 ...

  10. CHM文档打开空白的解决

    网上打包的CHM格式的文档,有时候打开无论点击目录哪一章节都会出现一片空白或者显示已取消到该网页的导航 这个情况的原因就是CHM文件在Windows的HTFS文件系统中会默认被阻止显示,解决方法就是在 ...