对象序列和反序列化Xml

 

1. XmlArray和XmlArrayItem

XmlArray和XmlArrayItem是不同的,XmlArray是指这个数组叫什么,XmlArrayItem 值数组的每个元素叫什么。

<XmlArray>
    <XmlArrayItem>0</XmlArrayItem>
    <XmlArrayItem>1</XmlArrayItem>
    <XmlArrayItem>2</XmlArrayItem>
</XmlArray>

 

测试对象

 1         #region UrlsInfo
 2         
 3         UrlsInfo info;
 4         public UrlsInfo ConfigInfo
 5         {
 6             private set{ info = value;}
 7             get { return info; }
 8         }
 9 
10         [Serializable]
11         [XmlRoot(ElementName = "root")]
12         /// <summary>
13         /// 
14         /// </summary>
15         public class UrlsInfo
16         {
17             [XmlElement(ElementName = "setting")]
18             public SettingInfo Settings
19             {
20                 get;
21                 set;
22             }
23             [XmlArray(ElementName = "urls"),XmlArrayItem(ElementName = "item")]
24             public List<string> Urls { get; set; }
25         }
26 
27         public class SettingInfo
28         {
29             [XmlElement(ElementName = "interval")]
30             /// <summary>
31             /// 心跳访问定时间隔
32             /// </summary>
33             public int Interval
34             {
35                 get;
36                 set;
37             }
38             [XmlElement(ElementName = "use_db")]
39             /// <summary>
40             /// 是否使用数据库;true或fasle,为true时,下面的urls节点无效,直接读取数据库的表记录;为fasle时读取下面的url节点的设置
41             /// </summary>
42             public bool UseDb
43             {
44                 get;
45                 set;
46             }
47             [XmlElement(ElementName = "connection_string")]
48             public string ConnectionString
49             {
50                 get;
51                 set;
52             }
53         }
54 
55         #endregion

 

单元测试  

 1         [TestMethod]
 2         public void TestUrlsInfo()
 3         {
 4             UrlsConfig info = new UrlsConfig();
 5             string ConfigPath = info.ConfigPath;
 6 
 7             UrlsConfig.UrlsInfo urlsInfo = new UrlsConfig.UrlsInfo(){
 8                 Settings = new UrlsConfig.SettingInfo(){
 9                     Interval = 60000,
10                     UseDb = false,
11                     ConnectionString = ""
12                 },
13                 Urls = new System.Collections.Generic.List<string>(){ "http://www.baidu.cn"}
14             };
15             string ConfigPath2 = AppDomain.CurrentDomain.BaseDirectory + "urlsTest.xml";
16             using (Stream stream = new FileStream(ConfigPath2, FileMode.OpenOrCreate, FileAccess.ReadWrite))
17             {
18                 XmlSerializer serializer = new XmlSerializer(typeof(UrlsConfig.UrlsInfo));
19                 serializer.Serialize(stream, urlsInfo);
20             }
21 
22            string xml =  WQB.Common.XmlHelper.SerializerPlain<UrlsConfig.UrlsInfo>(urlsInfo);
23 
24         }

 

 

 

相关参考:

https://www.mgenware.com/blog/?p=142

转载于:https://www.cnblogs.com/059212315/p/6763731.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值