Entity collections and entities in C # are converted to the corresponding string, XDocument, XElement, XDocument

Source: Internet
Author: User
Tags tostring

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>

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.