串行化自定义集合

1、CategoryList类代码:

 1 using  System;
 2 using  System.Collections;
 3 using  System.Linq;
 4 using  System.Web;
 5 using  System.Xml.Serialization;
 6
 7 ExpandedBlockStart.gifContractedBlock.gif /**/ /// <summary>
 8/// Summary description for CategoriesList
 9/// </summary>

10 public   class  CategoriesList
11 ExpandedBlockStart.gifContractedBlock.gif {
12    private ArrayList _categoriesList;
13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
14    /// Initializes a new instance of the CategoriesList class.
15    /// </summary>

16    public CategoriesList()
17ExpandedSubBlockStart.gifContractedSubBlock.gif    {
18        _categoriesList = new ArrayList();
19    }

20    public Category[] Categories
21ExpandedSubBlockStart.gifContractedSubBlock.gif    {
22        get
23ExpandedSubBlockStart.gifContractedSubBlock.gif        {
24            Category[] categories = new Category[_categoriesList.Count];
25            _categoriesList.CopyTo(categories);
26            return categories;
27        }

28        set
29ExpandedSubBlockStart.gifContractedSubBlock.gif        {
30            if (value == null)
31                return;
32            Category[] categories = (Category[])value;
33            _categoriesList.Clear();
34            foreach (Category cate in categories)
35ExpandedSubBlockStart.gifContractedSubBlock.gif            {
36                _categoriesList.Add(cate);
37            }
            
38        }
 
39    }

40    public int AddCategory(Category cate)
41ExpandedSubBlockStart.gifContractedSubBlock.gif    {
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 ExpandedBlockStart.gifContractedBlock.gif {
14    protected void Page_Load(object sender, EventArgs e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif    {
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 >

 

转载于:https://www.cnblogs.com/apiaceae/archive/2009/04/17/1437909.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值