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