C#实现复杂XML的序列化与反序列化
程序员文章站
2023-12-16 22:50:46
本文以一个实例的形式讲述了c#实现复杂xml的序列化与反序列化的方法。分享给大家供大家参考。具体方法如下:
已知.xml(再此命名default.xml)文件,请将其反序...
本文以一个实例的形式讲述了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); } } } }
ps:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线xml/json互相转换工具:
在线格式化xml/在线压缩xml:
xml在线压缩/格式化工具:
希望本文所述对大家的c#程序设计有所帮助。