- Serialization of two
Conversion of XML files to entity classes
I. Deserializing an XML file into an entity class object
1. The configuration information of the program is usually stored in a special configuration file (App.config/web.config) of the program or website. But now in order to demonstrate XML serialization and deserialization, the configuration information is saved in an XML file (config), and the configuration information is read out by deserialization to a separate class (Config.cs). This way, if you need to use configuration information, it is not necessary to read and write the XML file every time, just call the Config class to get the corresponding node information.
Config:
<?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> timed Scan database to read customer information through customer number and business number </ description> <CustomerInfos> <CustomerInfo> <customerid>0013</customerid > <BusinessId>03</BusinessId> </CustomerInfo> <CustomerInfo> < customerid>0022</customerid> <BusinessId>02</BusinessId> </CustomerInfo> </CustomerInfos> <ScanConfigs> <BeginTime>22:00:00</BeginTime> <EndTimme>23:00:00</EndTimme> </ScanConfigs></Config>
2. In order to convert the above XML to the desired entity class object, it is convenient to read the node data inside the program, and a corresponding entity class should be created, which is identified by attributes such as [Xmlroot][xmlelement][xmlattribute] in the entity 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 child node under the current parent node in the XML file//elementname is the name of the current node in the XML//class and the name of the corresponding XML node can be different (for example, in this class conf The attributes in IG clientdescription corresponding to the child nodes under the Root node config in the XML file Description) [XmlElement (elementname = "Description")] public String Clientdescription {get; set;} XmlAttribute indicates that this field is a property of the current node in the XML file [XmlAttribute (attributename= "Isauto")] public string Isauto {get; set; } [XmlElement (elementname = "Customerinfos")] public Customerinfos Customerinfos {get; Set } [XmlElement (elementname = "Scanconfigs")] public scanconfigs 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; SE T } [XmlElement (elementname = "Endtimme")] public string Endtimme {get; set;} }
3. The following code calls the method of the. NET XmlSerializer class to deserialize the XML
public class Xmlutil {/ /deserialization//Receive 2 parameters: Xmlfilepath (absolute path of the XML file to deserialize), type (which object type is deserialized 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;
two. Serialization
1. Conversely, an object of the Config class can be serialized as an XML file. The following code serializes an object into an XML file by calling the method of the. NET XmlSerializer class
public class Xmlutil {/ /serialize/Receive 4 parameters: Srcobject (Instance of object), type (object type), Xmlfilepath (absolute path of XML file after serialization), Xmlrootname (root node name in XML file)//When multiple object instances need to be serialized into the same XML file, Xmlrootname is the root node name common to all objects, and if not specified,. NET defaults to a name (arrayof+ Entity class name) public static void Serializetoxml (Object srcobject, Type type,string Xmlfilepath, string xmlrootname) { C4/>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 config = new config (); Config. Clientdescribe = "Timed scan of the database to read customer information through the customer number and business number."; Config. Isauto = "true"; CustomerInfo ci1 = new CustomerInfo (); Ci1. CustomerId = "0013"; Ci1. Businessid = "the"; CustomerInfo ci2 = new CustomerInfo (); Ci2. CustomerId = "0022"; Ci2. Businessid = "the"; 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);
Conversion between an XML file and an entity class