The method of using reflection to transform the model to the XML is universal.

Source: Internet
Author: User
Tags reflection

    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;     &nbsp         /// <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);    &nbsp     else     property. SetValue (Samplemodel, null, null);   }                   }              }               return SampleModel;         &nbsp}      }      #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.

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.