Category类的代码
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Web;
5
using System.Xml;
6
using System.Xml.Serialization;
7
8
/// <summary>
9
/// Summary description for Category
10
/// </summary>
11
///
12
13
[XmlRoot(Namespace="http://northwind.com/category")]
14
public 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
using System;2
using System.Collections.Generic;3
using System.Linq;4
using System.Web;5
using System.Xml;6
using System.Xml.Serialization;7

8
/// <summary>9
/// Summary description for Category10
/// </summary>11
/// 12

13
[XmlRoot(Namespace="http://northwind.com/category")]14
public class Category15
{16
public long CategoryID;17
public string CategoryName;18
public string Description;19

20
public Category()21
{22
//23
// TODO: Add constructor logic here24
//25
}26
}27

执行代码:
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;
9
using System.Xml.Serialization;
10
11
public 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
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;9
using System.Xml.Serialization;10

11
public 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
<?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




浙公网安备 33010602011771号