/// <summary>字符串转为Xml实体对象</summary> /// <param name="xml">Xml字符串</param> /// <param name="type">实体类型</param> /// <returns>Xml实体对象</returns> public static Object ToXmlEntity(this String xml, Type type) { if (xml.IsNullOrWhiteSpace()) { throw new ArgumentNullException("xml"); } if (type == null) { throw new ArgumentNullException("type"); } var x = new NewLife.Serialization.Xml { Stream = new MemoryStream(xml.GetBytes()) }; return(x.Read(type)); //if (!type.IsPublic) throw new XException("类型{0}不是public,不能进行Xml序列化!", type.FullName); //var serial = new XmlSerializer(type); //using (var reader = new StringReader(xml)) //using (var xr = new XmlTextReader(reader)) //{ // // 必须关闭Normalization,否则字符串的\r\n会变为\n // //xr.Normalization = true; // return serial.Deserialize(xr); //} }
/// <summary>数据流转为Xml实体对象</summary> /// <param name="stream">数据流</param> /// <param name="type">实体类型</param> /// <param name="encoding">编码</param> /// <returns>Xml实体对象</returns> public static Object ToXmlEntity(this Stream stream, Type type, Encoding encoding = null) { if (stream == null) { throw new ArgumentNullException("stream"); } if (type == null) { throw new ArgumentNullException("type"); } if (encoding == null) { encoding = Encoding.UTF8; } var x = new NewLife.Serialization.Xml { Stream = stream, Encoding = encoding }; return(x.Read(type)); //if (!type.IsPublic) throw new XException("类型{0}不是public,不能进行Xml序列化!", type.FullName); //var serial = new XmlSerializer(type); //using (var reader = new StreamReader(stream, encoding)) //using (var xr = new XmlTextReader(reader)) //{ // // 必须关闭Normalization,否则字符串的\r\n会变为\n // //xr.Normalization = true; // return serial.Deserialize(xr); //} }
/// <summary>序列化为Xml数据流</summary> /// <param name="obj">要序列化为Xml的对象</param> /// <param name="stream">目标数据流</param> /// <param name="encoding">编码</param> /// <param name="prefix">前缀</param> /// <param name="ns">命名空间,设为0长度字符串可去掉默认命名空间xmlns:xsd和xmlns:xsi</param> /// <param name="includeDeclaration">是否包含Xml声明 <?xml version="1.0" encoding="utf-8"?></param> /// <param name="attachComment">是否附加注释,附加成员的Description和DisplayName注释</param> /// <returns>Xml字符串</returns> public static void ToXml(this Object obj, Stream stream, Encoding encoding = null, String prefix = null, String ns = null, Boolean includeDeclaration = false, Boolean attachComment = false) { if (obj == null) { throw new ArgumentNullException("obj"); } if (encoding == null) { encoding = Encoding.UTF8; } // 删除字节序 encoding = encoding.TrimPreamble(); var xml = new NewLife.Serialization.Xml(); xml.Stream = stream; xml.Encoding = encoding; xml.UseAttribute = false; xml.UseComment = attachComment; xml.Write(obj); //var type = obj.GetType(); //if (!type.IsPublic) throw new XException("类型{0}不是public,不能进行Xml序列化!", type.FullName); //var serial = new XmlSerializer(type); //var setting = new XmlWriterSettings(); ////setting.Encoding = encoding.TrimPreamble(); //setting.Encoding = encoding; //setting.Indent = true; //// 去掉开头 <?xml version="1.0" encoding="utf-8"?> //setting.OmitXmlDeclaration = !includeDeclaration; //var p = stream.Position; //using (var writer = XmlWriter.Create(stream, setting)) //{ // if (ns == null) // serial.Serialize(writer, obj); // else // { // var xsns = new XmlSerializerNamespaces(); // xsns.Add(prefix, ns); // serial.Serialize(writer, obj, xsns); // } //} //if (attachComment) //{ // if (stream is FileStream) stream.SetLength(stream.Position); // stream.Position = p; // var doc = new XmlDocument(); // doc.Load(stream); // doc.DocumentElement.AttachComment(type); // stream.Position = p; // //doc.Save(stream); // using (var writer = XmlWriter.Create(stream, setting)) // { // doc.Save(writer); // } //} }
public void EnumTest() { var pm = new ModbusTcpParameter { Server = "127.0.0.1:502", Host = 1, ReadCode = FunctionCodes.ReadRegister, WriteCode = FunctionCodes.WriteRegister, }; var xml = new NewLife.Serialization.Xml { //Encoding = encoding, UseAttribute = false, UseComment = true }; xml.Write(pm); var str = xml.GetString(); Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?> <ModbusTcpParameter> <Host>1</Host> <ReadCode>ReadRegister</ReadCode> <WriteCode>WriteRegister</WriteCode> <Server>127.0.0.1:502</Server> <ProtocolId>0</ProtocolId> </ModbusTcpParameter>", str); var xml2 = new NewLife.Serialization.Xml { //Encoding = encoding, UseAttribute = false, UseComment = true, EnumString = false, }; xml2.Write(pm); var str2 = xml2.GetString(); Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?> <ModbusTcpParameter> <Host>1</Host> <ReadCode>3</ReadCode> <WriteCode>6</WriteCode> <Server>127.0.0.1:502</Server> <ProtocolId>0</ProtocolId> </ModbusTcpParameter>", str2); }
/// <summary>加载指定配置文件</summary> /// <param name="filename"></param> /// <returns></returns> public virtual Boolean Load(String filename) { if (filename.IsNullOrWhiteSpace()) { return(false); } filename = filename.GetFullPath(); if (!File.Exists(filename)) { return(false); } _loading = true; try { var data = File.ReadAllBytes(filename); var config = this as TConfig; Object obj = config; var xml = new NewLife.Serialization.Xml(); xml.Stream = new MemoryStream(data); xml.UseAttribute = false; xml.UseComment = true; if (_.Debug) { xml.Log = XTrace.Log; } if (!xml.TryRead(GetType(), ref obj)) { return(false); } config.ConfigFile = filename; config.SetExpire(); // 设定过期时间 config.OnLoaded(); return(true); } catch (Exception ex) { XTrace.WriteException(ex); return(false); } finally { _loading = false; } }
private String GetXml() { var xml = new NewLife.Serialization.Xml(); xml.Encoding = Encoding.UTF8; xml.UseAttribute = false; xml.UseComment = true; if (_.Debug) { xml.Log = XTrace.Log; } xml.Write(this); return(xml.GetString()); }
/// <summary>加载指定配置文件</summary> /// <param name="filename"></param> /// <returns></returns> public virtual Boolean Load(String filename) { if (filename.IsNullOrWhiteSpace()) { return(false); } filename = filename.GetFullPath(); if (!File.Exists(filename)) { return(false); } _loading = true; try { var config = this as TConfig; using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) { Object obj = config; var xml = new NewLife.Serialization.Xml(); xml.Stream = stream; xml.UseAttribute = false; xml.UseComment = true; if (!xml.TryRead(GetType(), ref obj)) { return(false); } } config.ConfigFile = filename; config.SetExpire(); // 设定过期时间 config.OnLoaded(); return(true); } catch (Exception ex) { XTrace.WriteException(ex); return(false); } finally { _loading = false; } }
/// <summary>序列化为Xml数据流</summary> /// <param name="obj">要序列化为Xml的对象</param> /// <param name="stream">目标数据流</param> /// <param name="encoding">编码</param> /// <param name="attachComment">是否附加注释,附加成员的Description和DisplayName注释</param> /// <returns>Xml字符串</returns> public static void ToXml(this Object obj, Stream stream, Encoding encoding = null, Boolean attachComment = false) { if (obj == null) { throw new ArgumentNullException("obj"); } if (encoding == null) { encoding = Encoding.UTF8; } // 删除字节序 //encoding = encoding.TrimPreamble(); var xml = new NewLife.Serialization.Xml(); xml.Stream = stream; xml.Encoding = encoding; xml.UseAttribute = false; xml.UseComment = attachComment; xml.Write(obj); }
/// <summary>序列化为Xml数据流</summary> /// <param name="obj">要序列化为Xml的对象</param> /// <param name="stream">目标数据流</param> /// <param name="encoding">编码</param> /// <param name="attachComment">是否附加注释,附加成员的Description和DisplayName注释</param> /// <param name="useAttribute">是否使用特性输出</param> public static void ToXml(this Object obj, Stream stream, Encoding encoding = null, Boolean attachComment = false, Boolean useAttribute = false) { if (obj == null) { return; } if (encoding == null) { encoding = Encoding.UTF8; } // 删除字节序 //encoding = encoding.TrimPreamble(); var xml = new NewLife.Serialization.Xml { Stream = stream, Encoding = encoding, UseAttribute = useAttribute, UseComment = attachComment }; xml.Write(obj); }