XML串行化

 

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

10 [Serializable]
11 public   class  Category
12 ExpandedBlockStart.gifContractedBlock.gif {
13    public long CategoryID;
14    public string CategoryName;
15    public string Description;
16    public Product[] Products;
17
18    public Category()
19ExpandedSubBlockStart.gifContractedSubBlock.gif    {           
20        //
21        // TODO: Add constructor logic here
22        //
23    }

24}

25

 

 

 

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

 9 public   class  Product
10 ExpandedBlockStart.gifContractedBlock.gif {
11    public long ProductID;
12    public string ProductName;
13    public string QuantityPerUnit;
14    public string UnitPrice;
15    public int UnitsInStock;
16
17    public Product()
18ExpandedSubBlockStart.gifContractedSubBlock.gif    {
19        //
20        // TODO: Add constructor logic here
21        //
22    }

23}

24

 

 

 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.IO;
 8 using  System.Xml.Serialization;
 9
10
11 public   partial   class  SimplySerialization : System.Web.UI.Page
12 ExpandedBlockStart.gifContractedBlock.gif {
13    protected void Page_Load(object sender, EventArgs e)
14ExpandedSubBlockStart.gifContractedSubBlock.gif    {
15        string xmlFilePath = @"C:\Data\Category.xml";
16        Category categoryObj = new Category();
17        categoryObj.CategoryID = 1;
18        categoryObj.CategoryName = "啤酒";
19        categoryObj.Description = "软饮料,咖啡,茶,啤酒和白酒";
20
21        Product prodObj = new Product();
22        prodObj.ProductID = 1;
23        prodObj.ProductName = "蔬菜";
24        prodObj.QuantityPerUnit = "10盒+20袋";
25        prodObj.UnitPrice = "18";
26        prodObj.UnitsInStock = 39;
27ExpandedSubBlockStart.gifContractedSubBlock.gif        Product[] products = { prodObj };
28        categoryObj.Products = products; 
29
30        XmlSerializer serializer = new XmlSerializer(typeof(Category));
31        TextWriter writer = new StreamWriter(xmlFilePath);
32        serializer.Serialize(writer, categoryObj);
33        writer.Close();
34        Response.Write("文件写入成功!");
35
36    }

37}

38

 Category.xml

转载于:https://www.cnblogs.com/apiaceae/archive/2009/04/15/1436823.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值