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