default.xml 文件如下:

<config>
<partnerships>
  <partnership name="Main_Listener">
    <loggers isAdditive="false">
      <logger type="file" name="Filelogger">
        <propertys>
        <property name="FileName" value="log\\outbound.log"/>
        <property name="MaxFileSize" value="10MB"/>
        <property name="MaxBackupIndex" value="10"/>
        <property name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
        </propertys>
      </logger>
      <logger type="file" name="Filelogger">
        <propertys>
        <property name="FileName" value="log\\outbound.log"/>
        <property name="MaxFileSize" value="10MB"/>
        <property name="MaxBackupIndex" value="10"/>
        <property name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
        </propertys>
      </logger>
    </loggers>
    <senders>
      <sender type="file">
        <propertys>
          <property name="PollingDir" value="WMSin"/>
          <property name="Interval" value="10"/>
          <property name="IsBackup" value="true"/>
          <property name="MaxFilenameLength" value="31"/>
          <property name="BackupDir" value="ARWMSin\\$date.yyyyMMdd$"/>
        </propertys>
      </sender>
      <sender type="file">
        <propertys>
          <property name="PollingDir" value="WMSin"/>
          <property name="Interval" value="50"/>
          <property name="IsBackup" value="true"/>
          <property name="MaxFilenameLength" value="33"/>
          <property name="BackupDir" value="ARWMSin\\$date.yyyyMMdd$"/>
        </propertys>
      </sender>
      <sender type="file">
        <propertys>
          <property name="PollingDir" value="WMSin"/>
          <property name="Interval" value="50"/>
          <property name="IsBackup" value="true"/>
          <property name="MaxFilenameLength" value="34"/>
          <property name="BackupDir" value="ARWMSin\\$date.yyyyMMdd$"/>
        </propertys>
      </sender>
    </senders>
    <receivers>
      <receiver type="exceed">
        <propertys>
          <property name="IsBackup" value="true"/>
          <property name="OriginalName" value="Backup\\In\\$date.yyyyMMdd$\\$Doctype$\\$Doctype$-$GUI$-$date.HHmmss$.ori.xml"/>
          <property name="ResponseName" value="Backup\\In\\$date.yyyyMMdd$\\$Doctype$\\$Doctype$-$GUI$-$date.HHmmss$.rsp.xml"/>
          <property name="ErrorName" value="inbound\\reprocess\\$Doctype$-$GUI$-$date.HHmmss$.err.xml"/>
        </propertys>
      </receiver>
    </receivers>
    <processors>
      <processor name="com.creaction.central.processor.EncodingProcessor">
        <propertys>
          <property name="FromEncoding" value="UTF-8"/>
          <property name="ToEncoding" value="GB2312"/>
        </propertys>
      </processor>
      <processor name="com.creaction.central.processor.ElemGetProcessor">
        <propertys>
          <property name="PropName" value="Doctype"/>
          <property name="TagName" value="Doctype"/>
        </propertys>
      </processor>
      <processor name="com.creaction.central.processor.ElemGetProcessor">
        <propertys>
          <property name="PropName" value="GUI"/>
          <property name="TagName" value="GUI"/>
        </propertys>
      </processor>
      <processor name="com.creaction.central.processor.XSLProcessor">
        <propertys>
          <property name="XSLFile" value=".\\transform\\$Doctype$.xsl"/>
        </propertys>
      </processor>
    </processors>
  </partnership>
 
</partnerships>
</config>

xml反序列化MModel封装各种类:

[XmlType(TypeName = "config")]
    public  class ConfigFileModel
    {
        [XmlArray("partnerships")]
        public List<partnership> partnerships { get; set; }
    }
    [XmlType(TypeName = "partnership")]
    public class partnership
    {
        [XmlAttribute]
        public string name { get; set; }

[XmlArray("loggers")]
        public List<logger> loggers { get; set; }

[XmlArray("senders")]
        public List<sender> senders { get; set; }

[XmlArray("receivers")]
        public List<receiver> receivers { get; set; }

[XmlArray("processors")]
        public List<processor> processors { get; set; }
    }

#region logger

[XmlType(TypeName = "logger")]
    public class logger
    {
        [XmlAttribute]
        public string isAdditive { get; set; }
        [XmlAttribute]
        public string type { get; set; }
        [XmlAttribute]
        public string name { get; set; }
        [XmlArray("propertys")]
        public List<property> propertys { get; set; }
    }

#endregion

#region sender
    [XmlType(TypeName = "sender")]
    public class sender
    {
        [XmlAttribute]
        public string type { get; set; }

[XmlArray("propertys")]
        public List<property> propertys { get; set; }
    }

#endregion

#region receiver

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

[XmlArray("propertys")]
        public List<property> propertys { get; set; }
    }

#endregion

#region processor
    [XmlType(TypeName = "processor")]
    public class processor
    {
        [XmlAttribute]
        public string type { get; set; }

[XmlArray("propertys")]
        public List<property> propertys { get; set; }
    }

#endregion

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

[XmlAttribute]
        public string value { get; set; }
    }

C# XML序列化和反序列化方法如下:

#region 指定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);
            }
        }
        #endregion

控制台程序执行:

static void Main(string[] args)
        {
            ConfigFileModel xml1 = CCERP.Base.Common.XmlHelper.DeserializeFromXml<ConfigFileModel>("default.xml");

Console.WriteLine(xml1.partnerships[0].processors[0].propertys[1].name);

Console.ReadLine();
        }

C# 复杂格式多级深度XML序列化反序列化的更多相关文章

  1. C# XML序列化/反序列化参考

    .NET提供了很不错的XML序列化/反序列化器,(它们所在的命名空间为System.Xml.Serialization)这是很方便的,下面对它的使用做一些总结,以供参考. 1,简单序列化 public ...

  2. C# Json库 和 xml 序列化反序列化 存在的问题

    json 正常情况下不会对私有成员进行序列化和反序列化, 因此在用json做深拷贝时, 就会丢失数据. 解决办法:       声明成公有成员. json在序列化和反序列化时, 如果类中有IComma ...

  3. php json与xml序列化/反序列化

    在web开发中对象的序列化与反序列化经常使用,比较主流的有json格式与xml格式的序列化与反序列化,今天想写个jsop的小demo,结果发现不会使用php序列化,查了一下资料,做个笔记 简单数组js ...

  4. XML序列化反序列化—常用类

    public class XMLSerializer    {        #region (public) xml序列化        /// <summary>        /// ...

  5. C# XML序列化/反序列化类XmlSerializer使用示例

    using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; ...

  6. XML序列化反序列化

    using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; nam ...

  7. XML序列化/反序列化数据库形式保存和读取。

    直接上码: 首先创建class1类 public class Class1 { public string name { get; set; } public int age { get; set; ...

  8. C# WPF xml序列化 反序列化

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  9. XML序列化与反序列化接口对接实战,看这篇就够了

    关键字:c# .NET XML 序列化 反序列化 本文为接口对接实践经验分享,不对具体的XML概念定义进行阐述:涉及工具类及处理方法已在生产环境使用多年,可放心使用.当然如果你发现问题,或有不同想法, ...

随机推荐

  1. android:json解析的两个工具:Gson和Jackson的使用小样例

    1.简单介绍 json是android与server通信过程中经常使用的数据格式,比如.例如以下是一个json格式的字符串: {"address":"Nanjing&qu ...

  2. Strut2------获取界面返回的session,application,parameter

    1.Action类下的代码 public class ServletActionDemo extends ActionSupport { @Override public String execute ...

  3. Java 基本语法----数组

    数组 数组概述 数组是多个相同类型数据的组合,实现对这些数据的统一管理. 数组属引用类型,数组型数据是对象(Object),数组中的每个元素相当于该对象的成员变量. 数组中的元素可以是任何数据类型,包 ...

  4. Redis(七)-- SpringMVC整合Redis集群

    1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  5. 【ecshop】调用购物车商品数量

    1 打开 includes/lib_insert.php 在最后位置添加如下代码: /** * 调用购物车商品数目 */ function insert_cart_mes_num() { $sql = ...

  6. web基础---->Fileupload文件的上传

    这里我们介绍文件上传的知识,使用的是apache的Commons FileUpload框架. 文件上传的使用 项目的部分结构如下: 一.使用Commons FileUpload的上传功能,我们需要引入 ...

  7. Linux学习(四)档案与目录管理

    1. 目录与路径  1.1 相对路径与绝对路径  1.2 目录的相关操作: cd, pwd, mkdir, rmdir  1.3 关于执行文件路径的变量: $PATH2. 档案与目录管理  2.1 档 ...

  8. COGS 1224. [SHOI2002]百事世界杯之旅(期望概率)

    COGS 1224. [SHOI2002]百事世界杯之旅 ★   输入文件:pepsi.in   输出文件:pepsi.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] ...

  9. 您需要安装旧 Java SE 6 运行环境才能打开“Eclipse”。

    mac删除jdk: sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk 旧版本sdk地址: http://www.oracle. ...

  10. [Linux]ssh相关问题

    ssh链接不上的最可能原因是防火墙没关,一般提示“connection refused”. 可以使用这个命令查看:#service iptables status 暂时关闭iptables,重启后还会 ...