From 65b4a4c37842188de065006f8812f5b73240c842 Mon Sep 17 00:00:00 2001 From: JohanLarsson Date: Wed, 12 Sep 2018 13:19:56 +0200 Subject: [PATCH] Support XmlArrayAttribute #3. --- .../XmlTests.SerializeWithXmlAttributes.cs | 8 +++++++ Gu.Xml/Writers/CollectionWriter.cs | 3 +-- .../ComplexValueWriter/ElementWriter.cs | 23 ++++++++++++------ Gu.Xml/Writers/EnumWriter{T}.cs | 24 +++++++++---------- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/Gu.Xml.Tests/XmlTests.SerializeWithXmlAttributes.cs b/Gu.Xml.Tests/XmlTests.SerializeWithXmlAttributes.cs index c60e140..c6f91e0 100644 --- a/Gu.Xml.Tests/XmlTests.SerializeWithXmlAttributes.cs +++ b/Gu.Xml.Tests/XmlTests.SerializeWithXmlAttributes.cs @@ -29,6 +29,7 @@ public class SerializeWithXmlAttributes new TestCaseData(new FieldWithXmlAttributeAttributeExplicitName { Value = 1 }), new TestCaseData(new With { Value = WithXmlEnumAttribute.One }), new TestCaseData(new With { Value = WithXmlEnumAttribute.Two }), + new TestCaseData(new WithXmlArrayAttribute { Ints = new[] { 1, 2, 3 } }), }; [TestCaseSource(nameof(Values))] @@ -95,6 +96,13 @@ public class FieldWithXmlElementAttributeExplicitName public int Value = 1; } + public class WithXmlArrayAttribute + { + [XmlArray(ElementName = "Numbers")] + [XmlArrayItem(ElementName = "Int32")] + public int[] Ints { get; set; } + } + public interface IValue { // ReSharper disable once UnusedMember.Global diff --git a/Gu.Xml/Writers/CollectionWriter.cs b/Gu.Xml/Writers/CollectionWriter.cs index c984a65..06d370f 100644 --- a/Gu.Xml/Writers/CollectionWriter.cs +++ b/Gu.Xml/Writers/CollectionWriter.cs @@ -95,8 +95,7 @@ private static void WriteItems(XmlWriter writer, IEnumerable enumerable) { foreach (var item in enumerable) { - var name = item == null ? "NULL" : RootName.Get(item.GetType()); - writer.WriteElement(name, item); + writer.WriteElement(item == null ? "NULL" : RootName.Get(item.GetType()), item); writer.WriteLine(); } } diff --git a/Gu.Xml/Writers/ComplexValueWriter/ElementWriter.cs b/Gu.Xml/Writers/ComplexValueWriter/ElementWriter.cs index 08f31b4..428a9fb 100644 --- a/Gu.Xml/Writers/ComplexValueWriter/ElementWriter.cs +++ b/Gu.Xml/Writers/ComplexValueWriter/ElementWriter.cs @@ -109,14 +109,11 @@ private static ElementWriter CreateWriter(stri private static bool TryGetElementName(MemberInfo member, out string name) { - name = null; - if (member.TryGetCustomAttribute(out System.Xml.Serialization.XmlElementAttribute xmlAttribute)) - { - name = xmlAttribute.ElementName ?? string.Empty; - } - else if (member.TryGetCustomAttribute(out System.Xml.Serialization.SoapElementAttribute soapAttribute)) + if (TryGetNameFromAttribute(member, x => x.ElementName, out name) || + TryGetNameFromAttribute(member, x => x.ElementName, out name) || + TryGetNameFromAttribute(member, x => x.ElementName, out name)) { - name = soapAttribute.ElementName ?? string.Empty; + return true; } if (name == null) @@ -140,5 +137,17 @@ private static bool TryGetElementName(MemberInfo member, out string name) return true; } + + private static bool TryGetNameFromAttribute(MemberInfo member, Func getName, out string name) + where TAttribute : Attribute + { + name = null; + if (member.TryGetCustomAttribute(out TAttribute attribute)) + { + name = getName(attribute); + } + + return !string.IsNullOrEmpty(name); + } } } \ No newline at end of file diff --git a/Gu.Xml/Writers/EnumWriter{T}.cs b/Gu.Xml/Writers/EnumWriter{T}.cs index ee737d7..ab6fc1b 100644 --- a/Gu.Xml/Writers/EnumWriter{T}.cs +++ b/Gu.Xml/Writers/EnumWriter{T}.cs @@ -31,6 +31,18 @@ public void Write(TextWriter writer, T value) writer.Write(this.cache.GetOrAdd(value, x => this.ToString(x))); } + private static bool TryGetNameFromAttribute(MemberInfo member, Func getName, out string name) + where TAttribute : Attribute + { + name = null; + if (member.TryGetCustomAttribute(out TAttribute attribute)) + { + name = getName(attribute); + } + + return !string.IsNullOrEmpty(name); + } + private string ToString(T value) { if (typeof(T).GetMember(value.ToString()).TryFirst(out var member)) @@ -44,17 +56,5 @@ private string ToString(T value) return Enum.Format(typeof(T), value, this.format).Replace(",", string.Empty); } - - private static bool TryGetNameFromAttribute(MemberInfo type, Func getName, out string name) - where T : Attribute - { - name = null; - if (type.TryGetCustomAttribute(out T attribute)) - { - name = getName(attribute); - } - - return !string.IsNullOrEmpty(name); - } } } \ No newline at end of file