Serialization and deserialization of multi-layer XML

Source: Internet
Author: User

Coding often encounters serialization of multi-level XML, such as the following, assuming the name is "Default. xml", which is in the same folder as 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>

We can define such an object class

[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; }    }

Next we will call the serialization and deserialization methods.

Class program {// deserialize public static t deserializefromxml <t> (string filepath) {try {If (! File. exists (filepath) throw new argumentnullexception (filepath + "nonexistent"); 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) ;}// serialize 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) {// If the relative path here is directly written as "default. XML "is incorrect, because the current file is actually in the bin folder xmlconfig xml = deserializefromxml <xmlconfig> (@".. \.. \ default. XML "); serializetoxml <xmlconfig> (@".. \.. \ test. XML ", XML); console. readkey ();}}

 

Serialization and deserialization of multi-layer XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.