let XML correspond to entity class one by one so that if you convert an entity into XML to write to the database, you do not need to add one by one nodes, in turn, read the XML of the database, and do not have to assign a value of one by one to model.
when translating entities into XML, you can use reflection to read all the attributes, and then assign values to the corresponding nodes of the XML. Conversely, when translating XML into an entity, it also uses reflection to traverse all nodes under the fixed node of the XML and assign values to the entity attributes. Here is the code that converts to each other. Because NULL is special, instead of [null], the value of [NULL] is not available in the Entity C # code #region model and XML conversion /// <summary> /// model conversion to XML /// </summary> /// <param name= "Model" > The model</param> to be transformed /// <returns></returns> public static string modeltoxml (Object model) { xmldocument xmldoc = new xmldocument (); xmlelement modelnode = xmldoc. CreateElement ("Model"); xmldoc. AppendChild (Modelnode); if (model != null) { foreach (Propertyinfo property in model. GetType (). GetProperties ()) { xmlelement attribute = xmldoc. CreateElement (property. Name); if ( Property. GetValue (model, null) != null) attribute. Innertext = property. GetValue (Model, null). ToString (); else attribute. innertext = "[Null]"; Modelnode.appendchild (attribute); } &NBSP;&NBSP} return xmldoc. OuterXml;   /// <summary> / xml conversion to model /// </summary> /// < Param name= "xml" > xml</param> /// <param name= to be transformed SampleModel ">model Entity example, the new one out can be </param> /// <returns></returns > public static object xmltomodel (string xml, object samplemodel) { if (String. IsNullOrEmpty (XML)) return SampleModel; else { xmldocument xmldoc = new xmldocument (); xmldoc. Loadxml (XML); XmlNodeList Attributes = xmldoc. selectSingleNode ("Model"). ChildNodes; foreach (xmlnode node in attributes) { foreach (propertyinfo property in Samplemodel.gettype (). GetProperties ()) { if (node. Name == property. Name) { if node. innertext != "[Null]") { if (property. Propertytype == typeof (System.Guid)) property. SetValue (node. Samplemodel, new guid. innertext), null); else property. SetValue (node. Samplemodel, convert.changetype. Innertext, property. propertytype), null);   else property. SetValue (Samplemodel, null, null); } } } return SampleModel;  } } #endregion
Why is this method so common? In the design of the database, many people will find that there is always a field design is not complete, or that the design was good, and then to change. For example, the description of the computer's property sheet, starting with only the desktop, and then add a notebook, and the notebook has a property "weight" is required, and the desktop has no relationship, it will be difficult to change the database. Using this method, you can put some irrelevant attributes in a field, the use of XML or nvarchar or ntext save, the program to write an entity class, save, converted to XML save, read out, the XML into model, so long as changes in entity classes, you can add and subtract attributes, And will retain the same property values as the original data without error.