对象序列和反序列化Xml

Posted 天晴了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象序列和反序列化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

以上是关于对象序列和反序列化Xml的主要内容,如果未能解决你的问题,请参考以下文章

序列化和反序列化代码

XML序列化和反序列化

go语言学习 ---struct 对象的序列化和反序列化(xml)

Json/XML序列化和反序列化

xstream实现对象的序列化和反序列化(Java)

csharp 此类演示了将对象序列化和反序列化为xml字符串的方法