缤纷多彩的植物信息世界

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

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

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

24}

25

 

 

 

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

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

23}

24

 

 

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Web;
 5using System.Web.UI;
 6using System.Web.UI.WebControls;
 7using System.IO;
 8using System.Xml.Serialization;
 9
10
11public partial class SimplySerialization : System.Web.UI.Page
12{
13    protected void Page_Load(object sender, EventArgs e)
14    {
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;
27        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

posted on 2009-04-15 23:34  虎克  阅读(284)  评论(0编辑  收藏  举报