asp.net利用对象数组实现xml序列化的代码实例
2011-07-22 11:18
821 查看
asp.net利用对象数组实现xml序列化的代码实例
数组的Xml序列化需要使用XmlArrayAttribute和XmlArrayItemAttribute;XmlArrayAttribute指定数组元素的Xml节点名,XmlArrayItemAttribute指定数组元素的Xml节点名。
如下代码示例:
/
*IT从业之家 http://www.3ppt.com */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace UseXmlSerialization
{
class Program
{
static void Main(string[] args)
{
//声明一个猫咪对象
var cWhite = new Cat { Color = "White", Speed = 10, Saying = "White or black, so long as the cat can catch mice, it is a good cat" };
var cBlack = new Cat { Color = "Black", Speed = 10, Saying =
"White or black, so long as the cat can catch mice, it is a good cat"
};
CatCollection cc = new CatCollection { Cats = new Cat[] { cWhite,cBlack} };
//序列化这个对象
XmlSerializer serializer = new XmlSerializer(typeof(CatCollection));
//将对象序列化输出到控制台
serializer.Serialize(Console.Out, cc);
Console.Read();
}
}
[XmlRoot("cats")]
public class CatCollection
{
[XmlArray("items"),XmlArrayItem("item")]
public Cat[] Cats { get; set; }
}
[XmlRoot("cat")]
public class Cat
{
//定义Color属性的序列化为cat节点的属性
[XmlAttribute("color")]
public string Color { get; set; }
//要求不序列化Speed属性
[XmlIgnore]
public int Speed { get; set; }
//设置Saying属性序列化为Xml子元素
[XmlElement("saying")]
public string Saying { get; set; }
}
}<SPAN style="FONT-FAMILY: verdana, Arial, Helvetica, sans-serif"
face="verdana, Arial, Helvetica, sans-serif"><SPAN
class=Apple-style-span style="FONT-SIZE: 14px; LINE-HEIGHT: 21px;
WHITE-SPACE: normal">
</SPAN></SPAN>
以上代码将输出:
<?xml version="1.0" encoding="gb2312"?>
<cats xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<items>
<item color="White">
<saying>White or black, so long as the cat can catch mice, it is a good
cat</saying>
</item>
<item color="Black">
<saying>White or black, so long as the cat can catch mice, it is a good
cat</saying>
</item>
</items>
</cats>
数组的Xml序列化需要使用XmlArrayAttribute和XmlArrayItemAttribute;XmlArrayAttribute指定数组元素的Xml节点名,XmlArrayItemAttribute指定数组元素的Xml节点名。
如下代码示例:
/
*IT从业之家 http://www.3ppt.com */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace UseXmlSerialization
{
class Program
{
static void Main(string[] args)
{
//声明一个猫咪对象
var cWhite = new Cat { Color = "White", Speed = 10, Saying = "White or black, so long as the cat can catch mice, it is a good cat" };
var cBlack = new Cat { Color = "Black", Speed = 10, Saying =
"White or black, so long as the cat can catch mice, it is a good cat"
};
CatCollection cc = new CatCollection { Cats = new Cat[] { cWhite,cBlack} };
//序列化这个对象
XmlSerializer serializer = new XmlSerializer(typeof(CatCollection));
//将对象序列化输出到控制台
serializer.Serialize(Console.Out, cc);
Console.Read();
}
}
[XmlRoot("cats")]
public class CatCollection
{
[XmlArray("items"),XmlArrayItem("item")]
public Cat[] Cats { get; set; }
}
[XmlRoot("cat")]
public class Cat
{
//定义Color属性的序列化为cat节点的属性
[XmlAttribute("color")]
public string Color { get; set; }
//要求不序列化Speed属性
[XmlIgnore]
public int Speed { get; set; }
//设置Saying属性序列化为Xml子元素
[XmlElement("saying")]
public string Saying { get; set; }
}
}<SPAN style="FONT-FAMILY: verdana, Arial, Helvetica, sans-serif"
face="verdana, Arial, Helvetica, sans-serif"><SPAN
class=Apple-style-span style="FONT-SIZE: 14px; LINE-HEIGHT: 21px;
WHITE-SPACE: normal">
</SPAN></SPAN>
以上代码将输出:
<?xml version="1.0" encoding="gb2312"?>
<cats xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<items>
<item color="White">
<saying>White or black, so long as the cat can catch mice, it is a good
cat</saying>
</item>
<item color="Black">
<saying>White or black, so long as the cat can catch mice, it is a good
cat</saying>
</item>
</items>
</cats>
相关文章推荐
- ASP.NET实现TreeView的XML数据源绑定实例代码
- asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码
- 利用XML序列化和Asp.Net Web缓存实现站点配置文件
- ASP.NET实现TreeView的XML数据源绑定实例代码
- asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码
- ASP.NET实现TreeView的XML数据源绑定实例代码
- asp.net C#如何实现当地时区转换的代码实例
- Asp.net C#下实现运行cmd调用命令的代码实例
- ASP.NET下使用xml反序列化、缓存依赖实现个性化配置文件的实时生效
- asp.net,pop3实现收邮件代码实例
- ASP.NET中利用DataList实现图片无缝滚动 实例分享
- asp.net中将数据库绑定到DataList控件的实现方法与实例代码
- asp.net实现上传文件显示本地绝对路径的实例代码
- ASP.NET+SQL Server利用XML实现动态菜单
- 利用ASP.NET服务器端自定义控件实现XML文件中还原表单
- ASP.NET获取MS SQL Server安装实例实现思路及代码
- ASP.NET下使用xml反序列化、缓存实现个性化配置文件的实时生效
- Asp.net利用JQuery AJAX实现无刷新评论思路与代码
- asp.net实现上传文件显示本地绝对路径的实例代码