public string Serialize <T>(T value) { var formatting = Option.Indent ? Formatting.Indented : Formatting.None; using (var sw = new StringWriter(CultureInfo.InvariantCulture)) using (var writer = new XmlTextWriter(sw) { Formatting = formatting }) using (var ser = new XmlSerialize(this, writer, Option)) { if (Option.Declaration) { writer.WriteStartDocument(); } ser.Serialize(value, Option.StartElement); if (Option.Declaration) { writer.WriteEndDocument(); } return(sw.ToString()); } }
/// <summary> /// 将对象转换为使用文本并写入到流中。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <param name="writer"></param> public void Serialize <T>(T value, XmlTextWriter writer) { using var ser = new XmlSerialize(this, writer, Option); if (Option.Declaration) { writer.WriteStartDocument(); } ser.Serialize(value, Option.StartElement); if (Option.Declaration) { writer.WriteEndDocument(); } }