1、CategoryList类代码:
1
using
System;
2 using System.Collections;
3 using System.Linq;
4 using System.Web;
5 using System.Xml.Serialization;
6
7 /**/ /// <summary>
8/// Summary description for CategoriesList
9/// </summary>
10 public class CategoriesList
11 {
12 private ArrayList _categoriesList;
13 /**//// <summary>
14 /// Initializes a new instance of the CategoriesList class.
15 /// </summary>
16 public CategoriesList()
17 {
18 _categoriesList = new ArrayList();
19 }
20 public Category[] Categories
21 {
22 get
23 {
24 Category[] categories = new Category[_categoriesList.Count];
25 _categoriesList.CopyTo(categories);
26 return categories;
27 }
28 set
29 {
30 if (value == null)
31 return;
32 Category[] categories = (Category[])value;
33 _categoriesList.Clear();
34 foreach (Category cate in categories)
35 {
36 _categoriesList.Add(cate);
37 }
38 }
39 }
40 public int AddCategory(Category cate)
41 {
42 return _categoriesList.Add(cate);
43
44 }
45
46}
47
2 using System.Collections;
3 using System.Linq;
4 using System.Web;
5 using System.Xml.Serialization;
6
7 /**/ /// <summary>
8/// Summary description for CategoriesList
9/// </summary>
10 public class CategoriesList
11 {
12 private ArrayList _categoriesList;
13 /**//// <summary>
14 /// Initializes a new instance of the CategoriesList class.
15 /// </summary>
16 public CategoriesList()
17 {
18 _categoriesList = new ArrayList();
19 }
20 public Category[] Categories
21 {
22 get
23 {
24 Category[] categories = new Category[_categoriesList.Count];
25 _categoriesList.CopyTo(categories);
26 return categories;
27 }
28 set
29 {
30 if (value == null)
31 return;
32 Category[] categories = (Category[])value;
33 _categoriesList.Clear();
34 foreach (Category cate in categories)
35 {
36 _categoriesList.Add(cate);
37 }
38 }
39 }
40 public int AddCategory(Category cate)
41 {
42 return _categoriesList.Add(cate);
43
44 }
45
46}
47
2、执行代码:
1
using
System;
2 using System.Collections;
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.Xml.Serialization;
9 using System.IO;
10
11
12 public partial class Default2 : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 string xmlFilePath = @"c:\data\Category.xml";
17 Category category1 = new Category();
18 category1.CategoryID = 1;
19 category1.CategoryName = "啤酒";
20 category1.Description = "软饮料,茶,白酒和红酒";
21 Category category2 = new Category();
22 category2.CategoryID = 2;
23 category2.CategoryName = "调料";
24 category2.Description = "酱油,醋,花椒";
25 CategoriesList list = new CategoriesList();
26 list.AddCategory(category1);
27 list.AddCategory(category2);
28 XmlSerializer serializer=new XmlSerializer(typeof(CategoriesList));
29 TextWriter writer = new StreamWriter(xmlFilePath);
30 serializer.Serialize(writer, list);
31 writer.Close();
32 Response.Write("文件写入成功!");
33
34 }
35}
36
2 using System.Collections;
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.Xml.Serialization;
9 using System.IO;
10
11
12 public partial class Default2 : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 string xmlFilePath = @"c:\data\Category.xml";
17 Category category1 = new Category();
18 category1.CategoryID = 1;
19 category1.CategoryName = "啤酒";
20 category1.Description = "软饮料,茶,白酒和红酒";
21 Category category2 = new Category();
22 category2.CategoryID = 2;
23 category2.CategoryName = "调料";
24 category2.Description = "酱油,醋,花椒";
25 CategoriesList list = new CategoriesList();
26 list.AddCategory(category1);
27 list.AddCategory(category2);
28 XmlSerializer serializer=new XmlSerializer(typeof(CategoriesList));
29 TextWriter writer = new StreamWriter(xmlFilePath);
30 serializer.Serialize(writer, list);
31 writer.Close();
32 Response.Write("文件写入成功!");
33
34 }
35}
36
3、输出XML文件:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < CategoriesList xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
3 < Categories >
4 < Category >
5 < CategoryID xmlns ="http://northwind.com/category" > 1 </ CategoryID >
6 < CategoryName xmlns ="http://northwind.com/category" > 啤酒 </ CategoryName >
7 < Description xmlns ="http://northwind.com/category" > 软饮料,茶,白酒和红酒 </ Description >
8 </ Category >
9 < Category >
10 < CategoryID xmlns ="http://northwind.com/category" > 2 </ CategoryID >
11 < CategoryName xmlns ="http://northwind.com/category" > 调料 </ CategoryName >
12 < Description xmlns ="http://northwind.com/category" > 酱油,醋,花椒 </ Description >
13 </ Category >
14 </ Categories >
15 </ CategoriesList >
2 < CategoriesList xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
3 < Categories >
4 < Category >
5 < CategoryID xmlns ="http://northwind.com/category" > 1 </ CategoryID >
6 < CategoryName xmlns ="http://northwind.com/category" > 啤酒 </ CategoryName >
7 < Description xmlns ="http://northwind.com/category" > 软饮料,茶,白酒和红酒 </ Description >
8 </ Category >
9 < Category >
10 < CategoryID xmlns ="http://northwind.com/category" > 2 </ CategoryID >
11 < CategoryName xmlns ="http://northwind.com/category" > 调料 </ CategoryName >
12 < Description xmlns ="http://northwind.com/category" > 酱油,醋,花椒 </ Description >
13 </ Category >
14 </ Categories >
15 </ CategoriesList >