Category类的代码
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Xml;
6using System.Xml.Serialization;
7
8/// <summary>
9/// Summary description for Category
10/// </summary>
11///
12
13[XmlRoot(Namespace="http://northwind.com/category")]
14public class Category
15{
16 public long CategoryID;
17 public string CategoryName;
18 public string Description;
19
20 public Category()
21 {
22 //
23 // TODO: Add constructor logic here
24 //
25 }
26}
27
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Xml;
6using System.Xml.Serialization;
7
8/// <summary>
9/// Summary description for Category
10/// </summary>
11///
12
13[XmlRoot(Namespace="http://northwind.com/category")]
14public class Category
15{
16 public long CategoryID;
17 public string CategoryName;
18 public string Description;
19
20 public Category()
21 {
22 //
23 // TODO: Add constructor logic here
24 //
25 }
26}
27
执行代码:
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;
9using System.Xml.Serialization;
10
11public partial class _Default : 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 XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
22 namespaces.Add("cate", "http://northwind.com/category");
23 XmlSerializer serializer = new XmlSerializer(typeof (Category));
24 TextWriter writer = new StreamWriter(xmlFilePath);
25 serializer.Serialize(writer, categoryObj, namespaces);
26 writer.Close();
27 Response.Write("文件写入成功!");
28
29 }
30}
31
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;
9using System.Xml.Serialization;
10
11public partial class _Default : 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 XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
22 namespaces.Add("cate", "http://northwind.com/category");
23 XmlSerializer serializer = new XmlSerializer(typeof (Category));
24 TextWriter writer = new StreamWriter(xmlFilePath);
25 serializer.Serialize(writer, categoryObj, namespaces);
26 writer.Close();
27 Response.Write("文件写入成功!");
28
29 }
30}
31
输出XML结果:
1<?xml version="1.0" encoding="utf-8"?>
2<cate:Category xmlns:cate="http://northwind.com/category">
3 <cate:CategoryID>1</cate:CategoryID>
4 <cate:CategoryName>啤酒</cate:CategoryName>
5 <cate:Description>软饮料,茶,可口可乐,白酒和红酒</cate:Description>
6</cate:Category>
7
2<cate:Category xmlns:cate="http://northwind.com/category">
3 <cate:CategoryID>1</cate:CategoryID>
4 <cate:CategoryName>啤酒</cate:CategoryName>
5 <cate:Description>软饮料,茶,可口可乐,白酒和红酒</cate:Description>
6</cate:Category>
7