.net XML的序列化

新建一个WebForm1.aspx页面

前台

View Code
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="usexml.WebForm1" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title></title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12
13 <asp:Button ID="Button1" runat="server" οnclick="Button1_Click"
14 Text="生成XML文档" />
15
16 </div>
17 </form>
18 </body>
19 </html>

后台

View Code
  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Xml;
8 using System.IO;
9 using System.Xml.Serialization;
10 namespace usexml
11 {
12 public partial class WebForm1 : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16
17 }
18
19
20 public class Cat
21 {
22
23 public string CatName;
24
25 public string CatSex;
26
27 [XmlArrayAttribute("Items")]
28 public catcolor[] CatColor;
29
30 public cattype CatType;
31
32
33
34 }
35
36 public class cattype
37 {
38 [XmlAttribute]
39 public string remark;
40
41 public string type1;
42
43 public string type2;
44
45 public string type3;
46
47
48
49
50
51 }
52
53
54 public class catcolor
55 {
56
57 public string color1;
58
59 public string color2;
60
61 public string color3;
62
63
64
65
66 }
67 /// <summary>
68 /// 序列化XML文档
69 /// </summary>
70 private void GetItems()
71 {
72
73
74
75 cattype billcattype = new cattype();
76
77 billcattype.remark = "猫的种类";
78
79 billcattype.type1 = "黑猫警长";
80
81 billcattype.type2 = "白猫警长";
82
83 billcattype.type3 = "黄猫警长";
84
85 catcolor billcatcolor = new catcolor();
86
87 billcatcolor.color1 = "";
88
89 billcatcolor.color2 = "";
90
91 billcatcolor.color3 = "";
92
93 Cat cat = new Cat();
94
95 cat.CatName = "";
96
97 cat.CatSex = "";
98
99 cat.CatType = billcattype;
100
101 catcolor[] items = { billcatcolor };
102 cat.CatColor = items;
103
104 //保存xml文档的路径
105 string patch = Server.MapPath("Cat.xml");
106
107 //用ser这个类创建一个XmlSerializer
108 XmlSerializer ser = new XmlSerializer(typeof(Cat));
109
110 try
111 {
112 //Stream提供一般的视图,这里将在目录下创建一个xml文档
113 Stream str = new FileStream(patch, FileMode.Create, FileAccess.Write);
114
115 //把 str 和 cat 传进 Serialize中,将xml文档序列化
116 ser.Serialize(str, cat);
117
118 //关闭资源
119 str.Close();
120 //释放资源
121 str.Dispose();
122
123 Response.Write("序列化成功!");
124
125
126 }
127
128 catch (Exception ex)
129 {
130
131 Response.Write(ex.Message);
132
133
134 }
135
136
137
138 finally
139 { }
140
141
142
143
144 }
145
146 /// <summary>
147 /// 点击按钮生成xml文档
148 /// </summary>
149 /// <param name="sender"></param>
150 /// <param name="e"></param>
151
152 protected void Button1_Click(object sender, EventArgs e)
153 {
154
155
156 //调用GetItems()方法
157 this.GetItems();
158
159
160
161 }
162 }
163 }

点击按钮,序列化XML文档

得到结果

View Code
 1 <?xml version="1.0"?>
2 <Cat xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3 <CatName />
4 <CatSex />
5 <Items>
6 <catcolor>
7 <color1>黑</color1>
8 <color2>白</color2>
9 <color3>黄</color3>
10 </catcolor>
11 </Items>
12 <CatType remark="猫的种类">
13 <type1>黑猫警长</type1>
14 <type2>白猫警长</type2>
15 <type3>黄猫警长</type3>
16 </CatType>
17 </Cat>




转载于:https://www.cnblogs.com/panan/archive/2011/12/11/2284052.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值