Skip to content

Commit

Permalink
Handle XmlRootAttribute. #3
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Sep 9, 2018
1 parent 1d5c968 commit 73e1752
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Gu.Xml.Tests/XmlTests.SerializeComplextTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Gu.Xml.Tests
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Serialization;
using NUnit.Framework;

public partial class XmlTests
Expand All @@ -13,6 +14,7 @@ public class SerializeComplexTypes
private static readonly TestCaseData[] Values =
{
new TestCaseData(new WithPublicMutableProperty { Value = 1 }),
new TestCaseData(new WithXmlRoot { Value = 1 }),
new TestCaseData(new WithPublicMutableFieldXmlElementExplicitName { Value = 1 }),
new TestCaseData(new WithPublicMutableField { Value = 1 }),
new TestCaseData(new WithPublicMutableFieldXmlElementExplicitName { Value = 1 }),
Expand Down Expand Up @@ -101,6 +103,12 @@ public class WithPublicMutableProperty
public int Value { get; set; } = 1;
}

[XmlRoot("Name")]
public class WithXmlRoot
{
public int Value { get; set; } = 1;
}

public class WithPublicMutablePropertyXmlElementExplicitName
{
[System.Xml.Serialization.XmlElement("Name")]
Expand Down
8 changes: 8 additions & 0 deletions Gu.Xml/Internals/RootName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Concurrent;
using System.Text;
using System.Xml.Serialization;

internal static class RootName
{
Expand All @@ -15,6 +16,13 @@ public static string Get(Type type)

private static string Create(Type type)
{
if (Attribute.GetCustomAttribute(type, typeof(XmlRootAttribute)) is XmlRootAttribute xmlRoot &&
xmlRoot.ElementName is string elementName &&
!string.IsNullOrEmpty(elementName))
{
return elementName;
}

if (!type.IsGenericType &&
!type.HasElementType)
{
Expand Down

0 comments on commit 73e1752

Please sign in to comment.