Conversion Between XML files and object classes

Source: Internet
Author: User
1. deserialize XML files into object class objects

1. The configuration information of a program is usually stored in a special configuration file (App. config/Web. config) of a program or website ). However, to demonstrate XML serialization and deserialization, the configuration information is saved in an XML file (config. XML), read the configuration information through deserialization and save it to a separate class (config. CS. In this way, if you need to use the configuration information, you do not need to read and write the XML file every time. You only need to call the config class to obtain the information of the corresponding node.

Config. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Config xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" isauto = "true">
<Description> periodically scans the database and reads the customer information through the customer number and business number. </description>
<Customerinfos> <customerinfo> <customerid> 0013 </customerid> <businessid> 03 </businessid> </customerinfo> <customerid> 0022 </customerid> <businessid> 02 </businessid> </customerinfo> </customerinfos>
<Scanconfigs> <begintime> 22:00:00 </begintime> <endtimme> 23:00:00 </endtimme> </scanconfigs>
</Config>

2. to convert the preceding XML to the desired object class object, you need to create a corresponding object class to read node data in the program, use [xmlroot] [xmlelement] [xmlattribute] and other attribute identifiers in the object class.

Config. CS:

// Xmlroot indicates that this class corresponds to the root node in the XML file.
[Xmlroot (elementname = "Config")] public classConfig{
// Xmlelement indicates that this field corresponds to a subnode under the current parent node in the XML file.
// Elementname is the name of the current node displayed in XML
// The Field Names in the class can be different from the corresponding XML node names (for example, the clientdescription attribute in the class config corresponds to the description of the child node under the root node config in the XML file) [xmlelement (elementname = "Description")] Public String clientdescription {Get; set ;}
// Xmlattribute indicates that this field is an attribute of the current node in the XML file [xmlattribute (attributename = "isauto")] Public String isauto {Get; set ;} [xmlelement (elementname = "customerinfos")] public mermerinfos customerinfos {Get; set;} [xmlelement (elementname = "scanconfigs")] public scanconfigs {Get; set ;}} public classCustomerinfos{[Xmlelement (elementname = "customerinfo")] public customerinfo [] CS {Get; Set ;}} public class customerinfo {[xmlelement (elementname = "customerid")] public String customerid {Get; set;} [xmlelement (elementname = "businessid")] Public String businessid {Get; Set ;}} public class scanconfigs {[xmlelement (elementname = "begintime")] Public String begintime {Get; set;} [xmlelement (elementname = "endtimme")] Public String endtimme {Get; set ;}}

3. the following code calls the. NET xmlserializer class method for XML deserialization.

Public class xmlutil {// deserialization
// Receives two parameters: xmlfilepath (absolute path of the XML file to be deserialized) and type (Object Type of the deserialization XML) public static object deserializefromxml (string xmlfilepath, type type) {object result = NULL; If (file. exists (xmlfilepath) {using (streamreader reader = new streamreader (xmlfilepath) {xmlserializer xs = new xmlserializer (type); Result = Xs. deserialize (Reader) ;}} return result ;}}

4. deserialization

   string xmlPath = "d:\\config.xml";    Config c = XmlUtil.DeserializeFromXml(xmlPath, typeof(Config)) as Config;
Ii. serialization

1. In turn, you can also serialize an object of the config class to an XML file. The following code serializes the object to an XML file by calling the xmlserializer class method of. net.

Public class xmlutil {// serialization
// Receives four parameters: srcobject (object instance), type (object type), xmlfilepath (absolute path of the serialized XML file), and xmlrootname (root node name in the XML file)
// When multiple object instances need to be serialized to the same XML file, xmlrootname is the name of the root node of all objects. If this parameter is not specified ,. net will default to a name (arrayof + object class name) public static void serializetoxml (Object srcobject, type, string xmlfilepath, string xmlrootname) {If (srcobject! = NULL &&! String. isnullorempty (xmlfilepath) {type = type! = NULL? Type: srcobject. GetType (); Using (streamwriter Sw = new streamwriter (xmlfilepath) {xmlserializer xs = string. isnullorempty (xmlrootname )? New xmlserializer (type): New xmlserializer (type, new xmlrootattribute (xmlrootname); Xs. serialize (SW, srcobject );}}}}

2. serialization

Config = new config (); config. clientdescribe = "regularly scans the database and reads the customer information through the customer number and business number. "; config. isauto = "true"; customerinfo CI1 = new customerinfo (); ci1.customerid = "0013"; ci1.businessid = "03"; customerinfo CI2 = new customerinfo (); ci2.customerid = "0022 "; ci2.businessid = "02"; customerinfos CIS = new customerinfos (); CIS. cs = new customerinfo [] {CI1, CI2}; config. customerinfos = CIS; scanconfigs SC = new scanconfigs (); SC. begintime = "22:00:00"; SC. endtimme = "23:00:00"; config. scanconfigs = SC; xmlutil. serializetoxml (config, config. getType (), "d: \ config. XML ", null );

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.