1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Web;
5
6
/**/
/// <summary>
7
/// Summary description for Category
8
/// </summary>
9
///
10
[Serializable]
11
public
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

2

3

4

5

6


7

8

9

10

11

12



13

14

15

16

17

18

19



20

21

22

23

24

25

1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Web;
5
6
/**/
/// <summary>
7
/// Summary description for Product
8
/// </summary>
9
public
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

2

3

4

5

6


7

8

9

10



11

12

13

14

15

16

17

18



19

20

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
{
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

2

3

4

5

6

7

8

9

10

11

12



13

14



15

16

17

18

19

20

21

22

23

24

25

26

27



28

29

30

31

32

33

34

35

36

37

38
