/// <summary> /// 创建默认文档 /// </summary> /// <param name="path"></param> public static void Creator(string path = @"..\\LoginAppConfig.xml") { AppConfig app = new AppConfig(); app.Flag = 0; app.MaxCharacters = 5; app.NumberOfWorlds = 1; app.ServerName = "C#端"; app.Userlimit = 100; app.WorldAddress = "127.0.0.1"; app.LoginPort = 8484; string xxx = Newtonsoft.Json.JsonConvert.SerializeObject(app); Console.WriteLine(xxx); XmlDocument xml = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(xxx, "Login"); XmlWriterSettings writerSetting = new XmlWriterSettings //声明编写器设置 { Indent = true, //定义xml格式,自动创建新的行 Encoding = UTF8Encoding.UTF8, //编码格式 OmitXmlDeclaration = true, //去掉版本号 }; XmlWriter writer = XmlWriter.Create(path, writerSetting); xml.Save(writer); }
public static bool CreateXML <T>(T obj, ref Stream xmlStream) { XmlWriterSettings writerSetting = new XmlWriterSettings //声明编写器设置 { Indent = true, //定义xml格式,自动创建新的行 Encoding = UTF8Encoding.UTF8, //编码格式 }; XmlSerializer xser = new XmlSerializer(typeof(T)); //实例化序列化对象 try { xser.Serialize(xmlStream, obj); //序列化对象到xml文档 using (MemoryStream ms = new MemoryStream()) { xmlStream.CopyTo(ms); // return ms.ToArray(); } } catch (Exception ex) { //_logServ.Error(string.Format("创建xml文档失败:{0}", ex.Message)); return(false); } finally { xmlStream.Close(); } return(true); }
/// <summary> /// 创建默认文档 /// </summary> /// <param name="path"></param> public static void Creator(string path = @"..\\ChannelAppConfig.xml") { AppConfig app = new AppConfig() { Bossdrop = 1, Drop = 1, EventMessage = "", WorldAddress = "127.0.0.1", Events = "", Exp = 1, Host = "127.0.0.1", Meso = 1, MountExp = 1, PetExp = 1, ServerMessage = "", }; string xxx = Newtonsoft.Json.JsonConvert.SerializeObject(app); Console.WriteLine(xxx); XmlDocument xml = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(xxx, "Channel"); XmlWriterSettings writerSetting = new XmlWriterSettings //声明编写器设置 { Indent = true, //定义xml格式,自动创建新的行 Encoding = UTF8Encoding.UTF8, //编码格式 OmitXmlDeclaration = true, //去掉版本号 }; XmlWriter writer = XmlWriter.Create(path, writerSetting); xml.Save(writer); }
private static void _xmlSerialToFile <T>(T obj, string filePath) { XmlWriter writer = null; //声明一个xml编写器 XmlWriterSettings writerSetting = new XmlWriterSettings //声明编写器设置 { Indent = true, //定义xml格式,自动创建新的行 Encoding = UTF8Encoding.UTF8, //编码格式 }; //创建一个保存数据到xml文档的流 writer = XmlWriter.Create(filePath, writerSetting); XmlSerializer xser = new XmlSerializer(typeof(T)); //实例化序列化对象 xser.Serialize(writer, obj); //序列化对象到xml文档 writer.Close(); }
/// <summary> /// 序列化类到xml文档 /// </summary> /// <typeparam name="T">类</typeparam> /// <param name="obj">类的对象</param> /// <param name="filePath">xml文档路径(包含文件名)</param> /// <returns>成功:true,失败:false</returns> public static bool CreateXML <T>(T obj, string filePath) { XmlWriter writer = null; //声明一个xml编写器 XmlWriterSettings writerSetting = new XmlWriterSettings //声明编写器设置 { Indent = true, //定义xml格式,自动创建新的行 Encoding = UTF8Encoding.UTF8, //编码格式 }; try { string xmlDirectory = Path.GetDirectoryName(filePath); if (!Directory.Exists(xmlDirectory)) { Directory.CreateDirectory(xmlDirectory); } //创建一个保存数据到xml文档的流 writer = XmlWriter.Create(filePath, writerSetting); } catch (Exception ex) { //_logServ.Error(string.Format("创建xml文档失败:{0}", ex.Message)); return(false); } XmlSerializer xser = new XmlSerializer(typeof(T)); //实例化序列化对象 try { xser.Serialize(writer, obj); //序列化对象到xml文档 } catch (Exception ex) { //_logServ.Error(string.Format("创建xml文档失败:{0}", ex.Message)); return(false); } finally { writer.Close(); } return(true); }
/// <summary> /// 序列化类到xml文档 /// </summary> /// <param name="order">要序列化的对象</param> /// <param name="filePath">xml文档路径(包含文件名)</param> /// <returns>成功:true,失败:false</returns> public bool Export(Order order, String filePath) { XmlWriter writer = null; //声明一个xml编写器 XmlWriterSettings writerSetting = new XmlWriterSettings //声明编写器设置 { Indent = true, //定义xml格式,自动创建新的行 Encoding = UTF8Encoding.UTF8, //编码格式 }; try { //创建一个保存数据到xml文档的流 writer = XmlWriter.Create(filePath, writerSetting); } catch (Exception ex) { Console.WriteLine($"创建xml文档失败:{0}", ex.Message); return(false); } XmlSerializer xser = new XmlSerializer(typeof(Order)); //实例化序列化对象 try { xser.Serialize(writer, order); //序列化对象到xml文档 } catch (Exception ex) { Console.WriteLine($"创建xml文档失败:{0}", ex.Message); return(false); } finally { writer.Close(); } return(true); }