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