最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net 对象数组的Xml序列化代码
时间:2022-06-25 06:35:26 编辑:袖梨 来源:一聚教程网
数组的Xml序列化需要使用XmlArrayAttribute和XmlArrayItemAttribute;XmlArrayAttribute指定数组元素的Xml节点名,XmlArrayItemAttribute指定数组元素的Xml节点名。
如下代码示例:
/
*玉开技术博客 https://www.111cn.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
相关文章
- 智谱清言网页版入口-智谱清言官网登录一键直达 02-12
- 看电影下载什么软件最好用免费-最好用的免费电影下载软件 02-12
- 秘塔AI搜索网页版-秘塔AI官网入口 02-12
- 火车票余票查询通道-火车票余票查询攻略 02-12
- 阿里云企业邮箱快捷登录入口-阿里云企业邮箱限免申请入口 02-12
- 抖音官网抖币充值苹果入口-抖音官网苹果充值入口在哪 02-12