你的位置:首页 > ASP.net教程

[ASP.net教程]C# 复杂格式多级深度XML序列化反序列化


 default.

 

<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.          <property name="ResponseName" value="Backup\\In\\$date.yyyyMMdd$\\$Doctype$\\$Doctype$-$GUI$-$date.HHmmss$.rsp.          <property name="ErrorName" value="inbound\\reprocess\\$Doctype$-$GUI$-$date.HHmmss$.err.        </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>

[    public  class ConfigFileModel
    {
        [        public List<partnership> partnerships { get; set; }
    }
    [    public class partnership
    {
        [        public string name { get; set; }

        [        public List<logger> loggers { get; set; }

        [        public List<sender> senders { get; set; }

        [        public List<receiver> receivers { get; set; }

        [        public List<processor> processors { get; set; }
    }

    #region logger

    [    public class logger
    {
        [        public string isAdditive { get; set; }
        [        public string type { get; set; }
        [        public string name { get; set; }
        [        public List<property> propertys { get; set; }
    }

    #endregion

    #region sender
    [    public class sender
    {
        [        public string type { get; set; }

        [        public List<property> propertys { get; set; }
    }

    #endregion

    #region receiver

    [    public class receiver
    {
        [        public string type { get; set; }

        [        public List<property> propertys { get; set; }
    }

    #endregion

    #region processor
    [    public class processor
    {
        [        public string type { get; set; }

        [        public List<property> propertys { get; set; }
    }

    #endregion


    [    public class property
    {
        [        public string name { get; set; }

        [        public string value { get; set; }
    }

 

C#

 

 

#region 指定         /// <summary>
        ///         /// </summary>
        /// <param name="filePath"></param>
        /// <param name="obj"></param>
        /// <param name="type"></param>
        public static void SerializeTo        {
            try
            {
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
                {
                    System.                    xs.Serialize(writer, obj);
                }
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 从某一        /// </summary>
        /// <param name="filePath">待反序列化的        /// <param name="type">反序列化出的</param>
        /// <returns></returns>
        public static T DeserializeFrom        {
            try
            {
                if (!System.IO.File.Exists(filePath))
                    throw new ArgumentNullException(filePath + " not Exists");

                using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
                {
                    System.                    T ret = (T)xs.Deserialize(reader);
                    return ret;
                }
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }
        #endregion

 

控制台程序执行:

  static void Main(string[] args)
        {
            ConfigFileModel
            Console.WriteLine(
            Console.ReadLine();
        }