C#C
C#3y ago
19 replies
Legion

❔ XML attributes with different name and type, but same type.Name

Hi! Here is a cut down of the problem i'm having:

using System.Xml.Serialization;

namespace test;
class Program
{
    private string _xml = @"<Tag Example1=""One"" Example2=""Two"" />";

    static void Main(string[] args)
    {
        var serializer = new XmlSerializer(typeof(Tag));
    }
}

public class Tag
{
    [XmlAttribute] public TestAttribute1.Example Example1;
    [XmlAttribute] public TestAttribute2.Example Example2;
}

public static class TestAttribute1
{
    public enum Example
    {
        One,
        Two
    }
}

public static class TestAttribute2
{
    public enum Example
    {
        One,
        Two
    }
}


And this is the error I'm getting:

System.InvalidOperationException: Types 'test.TestAttribute2.Example' and 'test.TestAttribute1.Example' both use the XML type name, 'Example', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type.


From what I understand, the problem the serializer is having is that it is only able to associate a type by its class name, not the whole name.

I'm not quite sure how to approach this, because the nested types may not be under my control.

I would have imagined that because its an attribute and not an element, that the conflict shouldn't matter.
Was this page helpful?