Using Reflection to convert the model and XML into one another is very common.

Source: Internet
Author: User

Let XML correspond to object classes one by one. In this way, if an object is converted into XML and written to the database, nodes are not added one by one. In turn, the XML of the database is read, you do not need to assign values to the model one by one. When you convert an object to XML, you can use reflection to read all attributes and assign values to the corresponding nodes of XML. In turn, reflection is also used when XML is converted to an object to traverse all nodes under the fixed node of XML and assign values to each object attribute. The following code is converted to each other. Because null is special, it is replaced by [null]. Therefore, [null] cannot be used in the object.

Mutual conversion between model and XML # mutual conversion between region model and XML
/** // <Summary>
/// Method for converting model to XML
/// </Summary>
/// <Param name = "model"> model to be converted </param>
/// <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 );
}
}

Return xmldoc. outerxml;
}

/** // <Summary>
/// Method for converting XML into Model
/// </Summary>
/// <Param name = "XML"> XML to be converted </param>
/// <Param name = "samplemodel"> entity example of the model. Just click New. </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 (samplemodel, new GUID (node. innertext), null );
Else
Property. setvalue (samplemodel, convert. changetype (node. innertext, property. propertytype), null );
}
Else
Property. setvalue (samplemodel, null, null );
}
}
}
Return samplemodel;
}
}
# Endregion

Why is this method generic? When designing a database, many people will find that the design of fields is incomplete, or that the design was completed and will be changed later. For example, to describe the properties table of a computer, we start to only consider the desktop. Later we need to add a notebook, while the notebook has a property "weight", which does not matter, changing the database is troublesome. However, this method can be used to store irrelevant attributes in a field in XML, nvarchar, or ntext format. An entity class is written in the program and converted to XML for saving, when reading the data, convert the XML into a model. As long as the object class is changed, the attributes can be added or subtracted, and the attribute values of the original data are retained without errors.

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.