zoukankan      html  css  js  c++  java
  • 对象序列和反序列化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

  • 相关阅读:
    C#---将数据库数据转换为json格式
    ASP.NET ---根据值让树中某一节点选中
    SQL---查询树中某个节点及其所有子节点
    CSS---相对定位笔记
    CSS---绝对定位笔记
    滑雪
    Self Numbers
    Lotto
    Parencodings
    Robot Motion
  • 原文地址:https://www.cnblogs.com/059212315/p/6763731.html
Copyright © 2011-2022 走看看