/// <summary> /// Basic serialization to an XML string using StringWriter in XmlSerializer /// </summary> /// <typeparam name="T"></typeparam> /// <param name="this"></param> /// <returns></returns> public static string ToXml <T>(this T @this) where T : class => Disposable .Using( () => new StringWriter(), writer => writer .Tee(w => @this.GetXmlSerializer().Serialize(w, @this)) .ToString() );
/// <summary> /// Serialization to an XML string with defined settings using XmlWriter(StringBuilder) in XmlSerializer /// </summary> /// <typeparam name="T"></typeparam> /// <param name="this"></param> /// <param name="settings"></param> /// <returns></returns> public static string ToXml <T>(this T @this, XmlWriterSettings settings) where T : class => new StringBuilder() .Tee(sb => Disposable. Using( () => sb.CreateXmlWriter(settings), writer => writer.Tee(w => @this.GetXmlSerializer().Serialize(writer, @this)) ) ) .ToString();
private static void Main(string[] args) { var selectBox = Disposable .Using( StreamFactory.GetStream, steam => new byte[steam.Length].Tee(b => steam.Read(b, 0, (int)steam.Length))) .Map(Encoding.UTF8.GetString) .Split(new[] { Environment.NewLine, }, StringSplitOptions.RemoveEmptyEntries) .Select((s, ix) => Tuple.Create(ix, s)) .ToDictionary(k => k.Item1, v => v.Item2) .Map(BuildSelectBox("theDoctors", true)) .Tee(Console.WriteLine); Console.ReadLine(); }
static void Main(string[] args) { // Transformation Pipeline Disposable .Using( StreamFactory.GetStream, stream => new byte[stream.Length].Tee(b => stream.Read(b, 0, (int)stream.Length))) .Map(Encoding.UTF8.GetString) .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) .Select((s, ix) => Tuple.Create(ix, s)) .ToDictionary(k => k.Item1, v => v.Item2) .Map(HtmlBuilder.BuildSelectBox("theDoctors", true)) .Tee(Console.WriteLine); // String Validation Pipeline "Doctor Who" .Map(Validator.IsNotNull) .Bind(Validator.IsNotEmpty) .Bind(Validator.MinLength(8)) .Map(result => result.IsSuccess ? result.SuccessValue : result.FailureValue) .Tee(Console.WriteLine); Console.ReadLine(); }
/// <summary> /// Basic deserialization of an XML string to an object of the given type /// </summary> /// <typeparam name="T"></typeparam> /// <param name="this"></param> /// <returns></returns> public static T FromXml <T>(this string @this) where T : class => Disposable .Using( () => new StringReader(@this), reader => new XmlSerializer(typeof(T)).Deserialize(reader) as T );