复杂 XML 的序列化反序列化_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 复杂 XML 的序列化反序列化

复杂 XML 的序列化反序列化

 2014/8/26 2:55:57  小尧弟  程序员俱乐部  我要评论(0)
  • 摘要:来自:http://q.cnblogs.com/q/65254/#a_144481很不错的一道实例,事先声明,没有http://q.cnblogs.com/u/583331/的提示,这种多层的复杂xml反序列化,我真没做出来。已知.xml(再此命名default.xml)文件,请将其反序列化到一个实例对象。1<?xmlversion="1.0"encoding="utf-8"?>2<config>3<rules>4<
  • 标签:XML 序列化

来自:http://q.cnblogs.com/q/65254/#a_144481

  很不错的一道实例,事先声明,没有http://q.cnblogs.com/u/583331/的提示, 这种多层的复杂xml反序列化,我真没做出来。


 

已知.xml(再此命名default.xml)文件,请将其反序列化到一个实例对象。

class="code_img_closed" src="/Upload/Images/2014082602/0015B68B3C38AA5B.gif" alt="" />
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <config>
 3   <rules>
 4     <rule name="namea">
 5       <params>
 6         <item name="p" type="get" pattern="(ios|android|)" />
 7         <item name="cid" type="get" pattern="(ios|android|)" />
 8         <item name="type" type="get" pattern="(ios|android|)" />
 9         <item name="sort" type="get" pattern="(ios|android|)" />
10         <item name="action" type="post" pattern="(ios|android|)" />
11         <item name="userAgent" type="header" pattern="(ios|android|)" />
12       </params>
13     </rule>
14  
15     <rule name="nameb">
16       <params>
17         <item name="p" type="get" pattern="(ios|android|)" />
18         <item name="cid" type="get" pattern="(ios|android|)" />
19         <item name="type" type="get" pattern="(ios|android|)" />
20         <item name="sort" type="get" pattern="(ios|android|)" />
21         <item name="action" type="post" pattern="(ios|android|)" />
22         <item name="userAgent" type="header" pattern="(ios|android|)" />
23       </params>
24     </rule>
25  
26     <rule name="namec">
27       <params>
28         <item name="p" type="get" pattern="(ios|android|)" />
29         <item name="cid" type="get" pattern="(ios|android|)" />
30         <item name="type" type="get" pattern="(ios|android|)" />
31         <item name="sort" type="get" pattern="(ios|android|)" />
32         <item name="action" type="post" pattern="(ios|android|)" />
33         <item name="userAgent" type="header" pattern="(ios|android|)" />
34       </params>
35     </rule>
36  
37     <rule name="named">
38       <params>
39         <item name="p" type="get" pattern="(ios|android|)" />
40         <item name="cid" type="get" pattern="(ios|android|)" />
41         <item name="type" type="get" pattern="(ios|android|)" />
42         <item name="sort" type="get" pattern="(ios|android|)" />
43         <item name="action" type="post" pattern="(ios|android|)" />
44         <item name="userAgent" type="header" pattern="(ios|android|)" />
45       </params>
46     </rule>
47   </rules>
48 </config>
Default.XML

 

C#示例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace 多层级反序列
{
    [XmlType(TypeName = "config")]
    public class XmlConfig
    {
        [XmlArray("rules")]
        public List<rule> rules { get; set; }
    }

    [XmlType(TypeName = "rule")]
    public class rule
    {
        [XmlAttribute]
        public string name { get; set; }

        [XmlArray("params")]
        public List<paramsItem> @params { get; set; }
    }

    [XmlType(TypeName = "item")]
    public class paramsItem
    {
        [XmlAttribute]
        public string name { get; set; }
        [XmlAttribute]
        public string type { get; set; }
        [XmlAttribute]
        public string pattern { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            /*
            XmlConfig xml1 = DeserializeFromXml<XmlConfig>("default.xml");
            XmlConfig xml = new XmlConfig();
            SerializeToXml<XmlConfig>("default.xml", xml);
            */

            XmlConfig xml1 = DeserializeFromXml<XmlConfig>("default.xml");

        }

        /// <summary>
        /// XML序列化某一类型到指定的文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="obj"></param>
        /// <param name="type"></param>
        public static void SerializeToXml<T>(string filePath, T obj)
        {
            try
            {
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    xs.Serialize(writer, obj);
                }
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 从某一XML文件反序列化到某一类型
        /// </summary>
        /// <param name="filePath">待反序列化的XML文件名</param>
        /// <param name="type">反序列化出的</param>
        /// <returns></returns>
        public static T DeserializeFromXml<T>(string filePath)
        {
            try
            {
                if (!System.IO.File.Exists(filePath))
                    throw new ArgumentNullException(filePath + " not Exists");

                using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    T ret = (T)xs.Deserialize(reader);
                    return ret;
                }
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }
    }
}
示例代码

 

发表评论
用户名: 匿名