使用XMLSerializerNamespace类生成受限的名称
生活随笔
收集整理的這篇文章主要介紹了
使用XMLSerializerNamespace类生成受限的名称
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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.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
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
?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
輸出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
?
轉載于:https://www.cnblogs.com/apiaceae/archive/2009/04/17/1437856.html
總結
以上是生活随笔為你收集整理的使用XMLSerializerNamespace类生成受限的名称的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 定制AjaxControlToolkit
- 下一篇: JavaScript中对象的构造方法