類、變量經常使用頭:javascript
[XmlRootAttribute]:對根節點的描述,在類聲明中使用 如:下例的Html類html
[XmlType]:對節點描述,在類聲明中使用 如:下例的Head類java
[XmlElement]:節點下內部節點描述,若是對數組標識,是對數組單元描述 如:下例的Html.body,Head.title,Head.metas和Head.scripts數組...jquery
[XmlAttribute]:節點下內部屬性描述 如:下例的Meat.httpequiv,Meat.content,Script.src,Script.type,...數組
[XmlArrayItem]:數組單元項描述 如:下例的Body.lisui
[XmlArray]:數組描述 如:下例的Body.lisspa
[XmlIgnore]:使該項不序列化 如:下例的Meta.datacode
[XmlText]:作爲節點的text文本輸出 如:下例的Script.content,Li.Content...xml
例如:htm
類定義代碼
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>