学习C# XmlSerializer 序列化反序列化XML

时间:2023-01-31 18:24:51

类、变量常用头:

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

例如:

类定义代码

学习C# XmlSerializer 序列化反序列化XML
 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 }
学习C# XmlSerializer 序列化反序列化XML

序列化

学习C# XmlSerializer 序列化反序列化XML
 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 }
学习C# XmlSerializer 序列化反序列化XML

serializerString值为:

学习C# XmlSerializer 序列化反序列化XML
<?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>