zoukankan      html  css  js  c++  java
  • C#实现复杂XML的序列化与反序列化

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

    Default.XML文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    
    <config> 
      <rules> 
        <rule name="namea"> 
          <params> 
            <item name="p" type="get" pattern="(ios|android|)"/>  
            <item name="cid" type="get" pattern="(ios|android|)"/>  
            <item name="type" type="get" pattern="(ios|android|)"/>  
            <item name="sort" type="get" pattern="(ios|android|)"/>  
            <item name="action" type="post" pattern="(ios|android|)"/>  
            <item name="userAgent" type="header" pattern="(ios|android|)"/> 
          </params> 
        </rule>  
        <rule name="nameb"> 
          <params> 
            <item name="p" type="get" pattern="(ios|android|)"/>  
            <item name="cid" type="get" pattern="(ios|android|)"/>  
            <item name="type" type="get" pattern="(ios|android|)"/>  
            <item name="sort" type="get" pattern="(ios|android|)"/>  
            <item name="action" type="post" pattern="(ios|android|)"/>  
            <item name="userAgent" type="header" pattern="(ios|android|)"/> 
          </params> 
        </rule>  
        <rule name="namec"> 
          <params> 
            <item name="p" type="get" pattern="(ios|android|)"/>  
            <item name="cid" type="get" pattern="(ios|android|)"/>  
            <item name="type" type="get" pattern="(ios|android|)"/>  
            <item name="sort" type="get" pattern="(ios|android|)"/>  
            <item name="action" type="post" pattern="(ios|android|)"/>  
            <item name="userAgent" type="header" pattern="(ios|android|)"/> 
          </params> 
        </rule>  
        <rule name="named"> 
          <params> 
            <item name="p" type="get" pattern="(ios|android|)"/>  
            <item name="cid" type="get" pattern="(ios|android|)"/>  
            <item name="type" type="get" pattern="(ios|android|)"/>  
            <item name="sort" type="get" pattern="(ios|android|)"/>  
            <item name="action" type="post" pattern="(ios|android|)"/>  
            <item name="userAgent" type="header" pattern="(ios|android|)"/> 
          </params> 
        </rule> 
      </rules> 
    </config>

    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);
                }
            }
        }
    }

    希望本文所述对大家的C#程序设计有所帮助。

  • 相关阅读:
    多线程学习纪要
    字符编码ASCII、Unicode、UTF-8以及验证
    C# 自定义特性Attribute要点
    Java --- 流操作
    数据库 --- 索引
    Java -- 枚举
    mybatis --- 使用通用mapper或者mybatis-plus进行映射时,当数据库中有大写字母,自定义映射的字段名。
    后端 --- 快速创建一个sb项目 (使用spring官网的https://start.spring.io)
    数据库 --- decimal类型测试
    Java --- 线上故障调优01 打印堆栈信息
  • 原文地址:https://www.cnblogs.com/bq-blog/p/xmloper2016.html
Copyright © 2011-2022 走看看