CODING中经常遇到序列化多层级的XML,例如下面这种,假设名字是“Default.xml”,和Program.cs在同一文件夹下
<?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>
我们可以定义这样一个实体类
[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 { //反序列化 public static T DeserializeFromXml<T>(string filePath) { try { if (!File.Exists(filePath)) throw new ArgumentNullException(filePath + "不存在"); using (StreamReader reader = new StreamReader(filePath)) { XmlSerializer xs = new XmlSerializer(typeof(T)); T ret = (T)xs.Deserialize(reader); return ret; } } catch (Exception ex) { return default(T); } } //序列化 public static void SerializeToXml<T>(string filePath, T obj) { try { using (StreamWriter writer = new StreamWriter(filePath)) { XmlSerializer xs = new XmlSerializer(typeof(T)); xs.Serialize(writer, obj); } } catch (Exception ex) { throw ex; } } static void Main(string[] args) { //这里的相对路径如果直接写“Default.xml”是错误的,因为当前文件实际是在bin文件夹下面 XmlConfig xml = DeserializeFromXml<XmlConfig>(@"..\..\Default.xml"); SerializeToXml<XmlConfig>(@"..\..\test.xml",xml); Console.ReadKey(); } }
时间: 2024-10-10 14:19:15