First, the preface
The last essay is mainly aimed at the matching and conversion between attribute attributes and entities of XML. The main content of this essay is to convert the object into corresponding XML and XElement. These 2 essays transform and match XML in different ways, and each match is operated in different angles. This article is mainly for the transformation of object entities, the next chapter focuses on the matching of XML.
Second, XML conversion
2.1 Entity Collection Transformation XML
Entity collections convert XML by: public static string toxml<t> (ilist<t> entities, string rootname = "") where T:new (), through an incoming entity collection object and the XML root name, which can be converted to the corresponding XML, as follows:
public static string toxml<t> (ilist<t> entities, string rootname = "") where T:new ()
{
if (entities = = NULL | | Entities. Count = = 0)
{return
string. Empty;
}
StringBuilder builder = new StringBuilder ();
Builder. Appendline (Xmlresource.xmlheader);
XElement element = toxelement<t> (entities, rootname);
Builder. Append (element. ToString ());
Return builder. ToString ();
}
For conversions to the entity collection, the result of the conversion is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<MapperInfoSet>
<MapperInfo>
<Name> Mapperinfoindex0</name>
<CreatedTime>2012-02-19T08:54:44.9411601+08:00</CreatedTime>
<IsActive>true</IsActive>
<Value>0</Value>
<percent>50</percent >
<targeturl>www.codeplex.com? id=0</targeturl>
</MapperInfo>
<MapperInfo>
<name>mapperinfoindex1</ name>
<CreatedTime>2012-02-19T08:54:44.9421602+08:00</CreatedTime>
<IsActive> false</isactive>
<Value>1</Value>
<Percent>50</Percent>
< Targeturl>www.codeplex.com? id=1</targeturl>
</MapperInfo>
</MapperInfoSet>
2.2 Entity Transformation XML
The method for entity transformation XML is: public static string toxml<t> (T entity) where T:new (), which can be converted to the corresponding XML by the incoming entity, as follows:
public static string toxml<t> (T entity) where t:new ()
{
if (entity = = null)
{return
string. Empty;
}
XElement element = toxelement<t> (entity);
return element. ToString ();
}
The conversion of a PIN to a single entity is as follows:
<MapperInfo>
<Name>MapperInfoIndex0</Name>
<createdtime>2012-02-19t08 :59:17.1387289+08:00</createdtime>
<IsActive>true</IsActive>
<value>0</ value>
<Percent>50</Percent>
<targeturl>www.codeplex.com? Id=0</targeturl>
</MapperInfo>