C#实现XML文件读取
程序员文章站
2023-12-18 19:01:04
本文实例为大家分享了c#实现xml文件读取的具体代码,供大家参考,具体内容如下
using system.collections;
using system.c...
本文实例为大家分享了c#实现xml文件读取的具体代码,供大家参考,具体内容如下
using system.collections; using system.collections.generic; using system.io; using system.text; using system.xml.serialization; /// <summary> /// 工具类 /// </summary> public static class tools { /// <summary> /// 存储数据 utf8 /// </summary> /// <param name="data">数据,自定义类</param> public static void savedata(gamedata data) { string filename = consts.datapath; // 文件名 // 文件流 stream stream = new filestream(filename, filemode.openorcreate, fileaccess.write); streamwriter sw = new streamwriter(stream, encoding.utf8); // utf8 xmlserializer xmlserializer = new xmlserializer(data.gettype()); // xml 文件序列化 xmlserializer.serialize(sw, data); sw.close(); stream.close(); } /// <summary> /// 读取数据 /// </summary> /// <returns>读取获得的数据</returns> public static gamedata getdatawithoutbom() { gamedata data = new gamedata(); stream stream = new filestream(consts.datapath, filemode.open, fileaccess.read); // 忽略标记 true streamreader sr = new streamreader(stream, true); xmlserializer xmlserializer = new xmlserializer(data.gettype()); data = xmlserializer.deserialize(sr) as gamedata; sr.close(); stream.close(); return data; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。