对象序列和反序列化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. 对象序列化的介绍.......................... ...
随机推荐
- python--io
import io # io模块里面主要使用StringIo和BytesIo # StringIo f = io.StringIO() # 像使用open函数创建文件一样,生成一个句柄 # 可以直接向 ...
- hihocoder Arithmetic Expression【在线查询】
Arithmetic Expression 时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you ...
- Python_Tips[4] -> and 和 or 的计算原则
and和or / and & or 对于and和or,可以连接多个值,其分别遵循原则: 全是 And: 返回第一个遇到的无效值,若全有效,返回最后一个有效值 全是 Or: 返回第一个遇到的有效 ...
- SPOJ 3267 DQUERY - D-query (主席树)(区间数的种数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- [POI2014]Supercomputer
题目大意: 给定一个$n(n\le10^6)$个结点的有根树,从根结点开始染色.每次可以染和已染色结点相邻的任意$k$个结点.$q(q\le10^6)$组询问,每次给定$k$,问至少需要染几次? 思路 ...
- lua异常捕获
解析json失败,想要捕获异常,可以使用pacll local cjson = require("cjson") local str = '[{"name":& ...
- 尝试解答java内存问题
在园子中看见了这个园友的问题,高手指点一下,关于编写Java程序让Jvm崩溃,恰巧这两天看了点相关的东西,也尝试了一下,下面是仁兄提出的第一个疑问,我来复现一下: package jvm; publi ...
- MySQL四种类型日志:Error Log、General Query Log、Binary Log、Slow Query Log
MySQL Server 有四种类型的日志——Error Log.General Query Log.Binary Log 和 Slow Query Log. 第一个是错误日志,记录mysqld的一些 ...
- Delphi TClientDataset查找定位功能
if CDSUserFunc.Locate('mod_id;res_id', VarArrayOf([UserFunc.MOD_ID, UserFunc.RES_ID]), [loCaseInsens ...
- IOS设置UIView的边框为圆角
iOS 系统自带的 View 组件都是正方形的,看起来都太生硬,有时候我需要变成圆角形式,如下图: 具体的实现是使用QuartzCore库,下面我具体的描述一下实现过程: • 首先 ...