最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net 对象数组的Xml序列化代码
时间:2011-07-22 编辑:简简单单 来源:一聚教程网
数组的Xml序列化需要使用XmlArrayAttribute和XmlArrayItemAttribute;XmlArrayAttribute指定数组元素的Xml节点名,XmlArrayItemAttribute指定数组元素的Xml节点名。
如下代码示例:
/
*玉开技术博客 http://www.111com.net */
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; }
}
}
以上代码将输出:
White or black, so long as the cat can catch mice, it is a good
cat
White or black, so long as the cat can catch mice, it is a good
cat
-
上一个: asp.net 自定义对象的Xml序列化
-
下一个: gridview全选与取值操作
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10