XML and List Mutual transfer classes

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Xml;namespacexmlhelper{/// <summary>    ///entity to Xml,xml to entity class/// </summary>    /// <typeparam name= "T" ></typeparam>     Public classXmlhelper<t>whereT:New()    {        #regionThe entity class is turned into XML/// <summary>        ///The object instance is turned into XML/// </summary>        /// <param name= "Item" >Object Instance</param>        /// <returns></returns>         Public Static stringentitytoxml (T item) {IList<T> items =NewList<t>(); Items.            ADD (item); returnentitytoxml (items); }        /// <summary>        ///The object instance set goes into XML/// </summary>        /// <param name= "items" >Object Instance Set</param>        /// <returns></returns>         Public Static stringEntitytoxml (ilist<t>items) {            //Create a XmlDocument documentXmlDocument doc =NewXmlDocument (); //creating a root elementXmlElement root = Doc. CreateElement (typeof(T). Name +"s"); //Add a child element set of a root element            foreach(T Iteminchitems)            {Entitytoxml (doc, root, item); }            //add a root element to a XmlDocument documentDoc.            AppendChild (root); returnDoc.        INNERXML; }        Private Static voidEntitytoxml (XmlDocument doc, XmlElement root, T item) {//Creating ElementsXmlElement Xmlitem = doc. CreateElement (typeof(T).            Name); //object's property setsystem.reflection.propertyinfo[] PropertyInfo=typeof(T). GetProperties (System.Reflection.BindingFlags.Public |System.Reflection.BindingFlags.Instance); foreach(System.Reflection.PropertyInfo pinfoinchpropertyInfo) {                if(pinfo! =NULL)                {                    //object property name                    stringName =pinfo.                    Name; //object property Values                    stringValue =String.Empty; if(pinfo. GetValue (Item,NULL) !=NULL) Value= pinfo. GetValue (Item,NULL). ToString ();//Get object property value//set the attribute value of an elementXmlitem.setattribute (name, value); }            }            //adding child elements to the rootRoot.        AppendChild (Xmlitem); }        #endregion        #regionXML into entity classes/// <summary>        ///XML into an object instance/// </summary>        /// <param name= "xml" >XML</param>        /// <returns></returns>         Public StaticT xmltoentity (stringxml) {IList<T> items =xmltoentitylist (XML); if(Items! =NULL&& items. Count >0)                returnitems[0]; Else return default(T); }        /// <summary>        ///XML into object instance set/// </summary>        /// <param name= "xml" >XML</param>        /// <returns></returns>         Public StaticIlist<t> Xmltoentitylist (stringxml) {XmlDocument doc=NewXmlDocument (); Try{doc.            LOADXML (XML); }            Catch            {                return NULL; }            if(Doc. Childnodes.count! =1)                return NULL; if(Doc. childnodes[0]. Name.tolower ()! =typeof(T). Name.tolower () +"s")                return NULL; XmlNode node= Doc. childnodes[0]; IList<T> items =NewList<t>(); foreach(XmlNode Childinchnode. ChildNodes) {if(Child. Name.tolower () = =typeof(T). Name.tolower ()) items.            ADD (xmlnodetoentity (child)); }            returnitems; }        Private StaticT Xmlnodetoentity (XmlNode node) {T item=NewT (); if(node. NodeType = =xmlnodetype.element) {XmlElement Element=(XmlElement) node; System.reflection.propertyinfo[] PropertyInfo=typeof(T). GetProperties (System.Reflection.BindingFlags.Public |System.Reflection.BindingFlags.Instance); foreach(XmlAttribute attrinchelement. Attributes) {stringAttrname =attr.                    Name.tolower (); stringAttrValue =attr.                    Value.tostring (); foreach(System.Reflection.PropertyInfo pinfoinchpropertyInfo) {                        if(pinfo! =NULL)                        {                            stringName =pinfo.                            Name.tolower (); Type DbType=pinfo.                            PropertyType; if(Name = =attrname) {                                if(String.IsNullOrEmpty (attrValue))Continue; Switch(Dbtype.tostring ()) { Case "System.Int32": pinfo. SetValue (item, Convert.ToInt32 (AttrValue),NULL);  Break;  Case "System.Boolean": pinfo. SetValue (item, Convert.toboolean (AttrValue),NULL);  Break;  Case "System.DateTime": pinfo. SetValue (item, Convert.todatetime (AttrValue),NULL);  Break;  Case "System.Decimal": pinfo. SetValue (item, Convert.todecimal (AttrValue),NULL);  Break;  Case "System.Double": pinfo. SetValue (item, convert.todouble (AttrValue),NULL);  Break; default: pinfo. SetValue (item, AttrValue,NULL);  Break; }                                Continue; }                        }                    }                }            }            returnitem; }        #endregion    }

XML and List Mutual transfer classes

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.