C#实现导出List数据到xml文件的方法【附demo源码下载】
程序员文章站
2023-01-30 08:53:35
本文实例讲述了c#实现导出list数据到xml文件的方法。分享给大家供大家参考,具体如下:
c#导出list数据到xml文件,这里主要用到的是:
xmlserializ...
本文实例讲述了c#实现导出list数据到xml文件的方法。分享给大家供大家参考,具体如下:
c#导出list数据到xml文件,这里主要用到的是:
xmlserializer 类 (system.xml.serialization)
将对象序列化到 xml 文档中和从 xml 文档中反序列化对象。xmlserializer 使您得以控制如何将对象编码到 xml 中。
实体类代码:
/// <summary> /// 用户实体类 /// /// 注意:类的访问修饰符必须是:public,否则会出现错误: /// 因其保护级别而不可访问。只能处理公共类型。 /// </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; } } }
导出代码:
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())) { xmlserializer xmlserializer = new xmlserializer(typeof(list<usermodel>)); xmlserializer.serialize(stringwriter, list); filestream fs = new filestream("list_usermodel.xml", filemode.openorcreate); streamwriter sw = new streamwriter(fs); sw.write(stringwriter.tostring()); sw.close(); fs.close(); messagebox.show("写入文件成功!"); } } catch (system.exception ex) { system.console.writeline("===================================="); system.console.writeline(ex.message); system.console.writeline(ex.stacktrace); } }
生成的xml文件内容:
<?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</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> <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> <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> <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> <password>password</password> </usermodel> <usermodel> <id>1</id> <name>name</name> <gender>0</gender> <age>20</age> <username>username</username> <password>password</password> </usermodel> </arrayofusermodel>
完整实例代码点击此处本站下载。
更多关于c#相关内容感兴趣的读者可查看本站专题:《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程》
希望本文所述对大家c#程序设计有所帮助。