void UniversalWriterAttributes(SerializationWriterContext ctx, object instance, IEnumerable <Element> elements) { // PROPERTY as ATTRIBUTE: // primitive // composite null foreach (var element in elements.OrderBy(x => x.Name)) { var val = element.GetValue(instance); if (IsInAttrib(val)) { if (element.DefaultValueAttribute == null || !Equals(element.DefaultValueAttribute.Value, val)) { if (ReferenceEquals(null, val)) { var x = ctx.GetAliasFor("http://schemas.microsoft.com/winfx/2006/xaml"); val = $"{{{x}:Null}}"; } val = ConvertToString(val); ctx.Write($@" {element.Name}=""{val}"""); } } } }
void UniversalWriter(SerializationWriterContext ctx, object instance) { var type = instance.GetType(); var meta = GetTypeMetada(type); var elements = meta.Elements; ctx.NamespacesUsed.Add(meta.XmlNamespace); string alias; if (ctx.Indentation > 0 && meta.XmlNamespace != ctx.RootNamespace || meta.IsX()) { alias = ctx.GetAliasFor(meta.XmlNamespace) + ':'; } else { alias = null; ctx.RootNamespace = meta.XmlNamespace; } if (ctx.Indentation != 0 && ctx.Result[ctx.Result.Length - 1] != '\n') { ctx.Result.AppendLine(); } ctx.Write($"{Indentation(ctx.Indentation)}<{alias}{type.Name}"); UniversalWriterAttributes(ctx, instance, elements); // <nested in new builder> var subCtx = ctx.Nested(); UniversalWriterSubnodes(subCtx, instance, elements); // </nested in new builder> if (!ctx.XmlnsEmited && ctx.Indentation == 0) { if (!string.IsNullOrEmpty(ctx.RootNamespace)) { ctx.Write($" xmlns=\"{ctx.RootNamespace}\""); } foreach (var ns in ctx.NamespacesUsed) { if (ns != ctx.RootNamespace) { ctx.Write($" xmlns:{ctx.GetAliasFor(ns)}=\"{ns}\""); } } ctx.XmlnsEmited = true; } // <body> string body = null; if (meta.IsBody()) { body = string.Format(CultureInfo.InvariantCulture, "{0}", instance); } // </body> if (subCtx.Result.Length > 0) { ctx.WriteLine(">"); ctx.Write(subCtx.Result.ToString()); ctx.Write($"{Indentation(ctx.Indentation)}</{alias}{type.Name}>"); } else if (body != null) { ctx.Write(">"); ctx.Write(body); ctx.Write($"</{alias}{type.Name}>"); } else { ctx.Write(" />"); } if (ctx.Indentation != 0) { ctx.Result.AppendLine(); } }