C # Exports the list data to an XML file, where the main use is:
XmlSerializer Class (System.Xml.Serialization)
xmlserializer allows you to control how objects are encoded into XML.
Entity Class Code:
<summary>///User entity class//////////////////////////NOTE: The class must have the access modifier: public, otherwise an error occurs: Only public types can be processed. </summary> [Serializable] public class Usermodel {private Long id = 1; private string name = "Name"; private int gender = 0; private int age = 20; private string userName = "UserName"; private string password = "password"; Public long ID {get {return Id;} set {id = value;} } public string Name {get {return Name;} set {name = value;} } public int Gender {get {return Gender;} set {gender = value;} } public int Age {get {return-age;} set {age = value;} } public string UserName {get {return UserName;} set {userName = value;} } public string Password {get {retUrn password; } set {password = value;} } }
Export code:
private void Button1_Click (object sender, EventArgs e) {try {list< ; usermodel> list = new list<usermodel> (); List. ADD (New Usermodel ()); List. ADD (New Usermodel ()); List. ADD (New Usermodel ()); List. ADD (New Usermodel ()); List. ADD (New Usermodel ()); List. ADD (New Usermodel ()); List. ADD (New Usermodel ()); List. ADD (New Usermodel ()); List. ADD (New Usermodel ()); List. ADD (New Usermodel ()); using (System.IO.StringWriter StringWriter = new StringWriter (new StringBuilder ())) {Xm Lserializer XmlSerializer = new XmlSerializer (typeof (List<usermodel>)); Xmlserializer.serialize (StringWriter, list); FileStream fs = new FileStream ("List_usermodel.xml", FileMode.OpenOrCreate); StreamWriterSW = new StreamWriter (FS); Sw. Write (Stringwriter.tostring ()); Sw. Close (); Fs. Close (); MessageBox.Show ("Write file succeeded! "); }} catch (System.Exception ex) {System.Console.WriteLine ("============== ======================"); System.Console.WriteLine (ex. Message); System.Console.WriteLine (ex. StackTrace); } }
Content of the generated XML file:
<?xml version= "1.0" encoding= "utf-16"? ><arrayofusermodel xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "> <UserModel> <Id>1</Id> < name>name</name> <Gender>0</Gender> <Age>20</Age> <username>username</u sername> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <username>usernam e</username> <Password>password</Password> </UserModel> <UserModel> <id>1</ id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName> username</username> <Password>password</Password> </UserModel> <UserModel> <id> ;1</id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <passwor d>password</password> </UserModel> <UserModel> <Id>1</Id> <name>name</na me> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> < password>password</password> </UserModel> <UserModel> <Id>1</Id> <name>name </Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <name& gt;name</name> <Gender>0</Gender> <Age>20</Age> <username>username</userna me> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> &L T name>name</name&Gt <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <PASSWORD&G t;password</password> </UserModel> <UserModel> <Id>1</Id> <name>name</name& Gt <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <PASSWORD&G t;password</password> </UserModel> <UserModel> <Id>1</Id> <name>name</name& Gt <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <PASSWORD&G T;password</password> </UserModel></ArrayOfUserModel>