对象序列和反序列化Xml
1. XmlArray和XmlArrayItem
XmlArray和XmlArrayItem是不同的,XmlArray是指这个数组叫什么,XmlArrayItem 值数组的每个元素叫什么。
<XmlArray>
<XmlArrayItem>0</XmlArrayItem>
<XmlArrayItem>1</XmlArrayItem>
<XmlArrayItem>2</XmlArrayItem>
</XmlArray>
测试对象
#region UrlsInfo
UrlsInfo info;
public UrlsInfo ConfigInfo
{
private set{ info = value;}
get { return info; }
}
[Serializable]
[XmlRoot(ElementName = "root")]
/// <summary>
///
/// </summary>
public class UrlsInfo
{
[XmlElement(ElementName = "setting")]
public SettingInfo Settings
{
get;
set;
}
[XmlArray(ElementName = "urls"),XmlArrayItem(ElementName = "item")]
public List<string> Urls { get; set; }
}
public class SettingInfo
{
[XmlElement(ElementName = "interval")]
/// <summary>
/// 心跳访问定时间隔
/// </summary>
public int Interval
{
get;
set;
}
[XmlElement(ElementName = "use_db")]
/// <summary>
/// 是否使用数据库;true或fasle,为true时,下面的urls节点无效,直接读取数据库的表记录;为fasle时读取下面的url节点的设置
/// </summary>
public bool UseDb
{
get;
set;
}
[XmlElement(ElementName = "connection_string")]
public string ConnectionString
{
get;
set;
}
}
#endregion
单元测试
[TestMethod]
public void TestUrlsInfo()
{
UrlsConfig info = new UrlsConfig();
string ConfigPath = info.ConfigPath; UrlsConfig.UrlsInfo urlsInfo = new UrlsConfig.UrlsInfo(){
Settings = new UrlsConfig.SettingInfo(){
Interval = ,
UseDb = false,
ConnectionString = ""
},
Urls = new System.Collections.Generic.List<string>(){ "http://www.baidu.cn"}
};
string ConfigPath2 = AppDomain.CurrentDomain.BaseDirectory + "urlsTest.xml";
using (Stream stream = new FileStream(ConfigPath2, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
XmlSerializer serializer = new XmlSerializer(typeof(UrlsConfig.UrlsInfo));
serializer.Serialize(stream, urlsInfo);
} string xml = WQB.Common.XmlHelper.SerializerPlain<UrlsConfig.UrlsInfo>(urlsInfo); }
相关参考:
https://www.mgenware.com/blog/?p=142
对象序列和反序列化Xml的更多相关文章
- .NET(C#)使用Serialize、Deserialize序列和反序列化XML文档
本文给大家分享一下C#操作(读取.写入)XML文档的实用方法,即用.NET本身提供的Deserialize和Serialize进行反序列化和序列化XML文档.这种方法主要是对比较规范的XML文档进行操 ...
- C# xml数组的序列和反序列化
先来看xml <?xml version="1.0"?> <root xmlns:xsi="http://www.w3.org/2001/XMLSche ...
- C# XML对象序列化、反序列化
XML 序列化:可以将对象序列化为XML文件,或者将XML文件反序列化为对象还有种方法使用LINQ TO XML或者反序列化的方法从XML中读取数据. 最简单的方法就是.net framework提供 ...
- C# XML对象序列化、反序列化 - PEPE YU
http://www.tuicool.com/articles/IjE7ban http://www.cnblogs.com/johnsmith/archive/2012/12/03/2799795. ...
- C#对象序列化成XML,以及自定义标签名
C#对象序列化操作: public class XMLHelper { /// <summary> /// 对象序列化成 XML String /// </summary> p ...
- 匿名对象序列化为XML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- 将Java对象序列化成JSON和XML格式
1.先定义一个Java对象Person: public class Person { String name; int age; int number; public String getName() ...
- C#对象序列化与反序列化zz
C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍........................ ...
- C#对象序列化与反序列化
C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍.......................... ...
随机推荐
- CF985B Switches and Lamps【矩阵操作/枚举】
[链接]CF985B [题意]:给n盏灯,m个开关,每次按开关只能将灯从灯灭的状态转变为灯亮,问是否存在不按所有开关就将所有灯打开的方法. [分析]:有两种办法,一种代码复杂点,容易想到枚举去掉每一行 ...
- CF985A Chess Placing【思维】
[链接]:CF985A [题意]:给你n和n/2个数ai,每个ai和奇数.偶数比较距离(注意选了奇数,偶数的距离就不要算了,反之同理),求最小的答案. [代码]: #include <iostr ...
- RandomeAccessFile - read write
RandomeAccessFile use write replace writeBytes public class RandomAccessFileTest { public static voi ...
- IO 最快的read 和 write
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; impo ...
- ARC 098 C - Attention
Problem Statement There are N people standing in a row from west to east. Each person is facing east ...
- 设置iframe高度自适应屏幕高度
写在前面: 最近在搭建项目前台页面框子的时候,把iframe设置成了固定的高度,导致不同的电脑尺寸访问的时候,高度差异较大,故查了下,将iframe设置成自动适应屏幕高度的方式,这里记录下. 还是直接 ...
- 使用ARC也可能发生内存泄露
1,循环参照 A有个属性参照B,B有个属性参照A,如果都是strong参照的话,两个对象都无法释放. 这种问题常发生于把delegate声明为strong属性了. 例, @interface Samp ...
- WPF--TextBox的验证
WPF--TextBox的验证
- 【视频】Linux高级程序设计01.3命令行选项及参数
[课程笔记] [命令行参数] 选项:-l -a -i 参数:-l /home main 函数形式: int main(int argc, char *argv[]) main函数是有参数的,而且有返回 ...
- 提高MSSQL数据库性能(1)对比count(*) 和 替代count(*)
原文:提高MSSQL数据库性能(1)对比count(*) 和 替代count(*) 文章准备的数据库: Atricles 表 数据量60690000条数据 ArticleID 主键自增列+自动建立 ...