C
C#14mo ago
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
}
}
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.
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.
10 Replies
JakenVeina
JakenVeina14mo ago
this is probably more an issue of the fact that you're trying to model entire objects as attributes the proper model should be... oh, wait, I see what you did so, yeah, the error tells you what to do specify a unique name for each of those enum types, directly the default behavior for generating type names in XML-land doesn't take into account nested type names in C#-land
Legion
LegionOP14mo ago
thanks, do i specify it against the enum or the property?
JakenVeina
JakenVeina14mo ago
not sure possibly within the [XmlAttribute] tag you already have? otherwise, some new tag on each of the enums themselves
Legion
LegionOP14mo ago
i think it means this:
public static class TestAttribute1
{
[XmlType(TypeName = "TestAttribute1Example")]
public enum Example
{
One,
Two
}
}

public static class TestAttribute2
{
[XmlType(TypeName = "TestAttribute2Example")]
public enum Example
{
One,
Two
}
}
public static class TestAttribute1
{
[XmlType(TypeName = "TestAttribute1Example")]
public enum Example
{
One,
Two
}
}

public static class TestAttribute2
{
[XmlType(TypeName = "TestAttribute2Example")]
public enum Example
{
One,
Two
}
}
this does work :) but that creates another problem, in my application i dont have access to these nested enums, so i need to find a way to apply these TypeNames retroactively
JakenVeina
JakenVeina14mo ago
so, these are third-party types you're trying to deserialize?
Legion
LegionOP14mo ago
yes, but for more complex types that dont have [Serializable] i have other solutions, my current blocker is the ambiguous type names
JakenVeina
JakenVeina14mo ago
for types that you can't annotate, and that can't just be deserialized by inference, you'll probably be forced to write a custom deserializer or type converter
Legion
LegionOP14mo ago
static void Main(string[] args)
{
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
attrOverrides.Add(typeof(TestAttribute1.Example), new() { XmlType = new XmlTypeAttribute(typeName: "TestAttribute1Example") });
attrOverrides.Add(typeof(TestAttribute2.Example), new() { XmlType = new XmlTypeAttribute(typeName: "TestAttribute2Example") });

var serializer = new XmlSerializer(typeof(Tag), attrOverrides);

Tag tag = (Tag)serializer.Deserialize(new StringReader(_xml));

Console.WriteLine(tag.Example2);
}
static void Main(string[] args)
{
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
attrOverrides.Add(typeof(TestAttribute1.Example), new() { XmlType = new XmlTypeAttribute(typeName: "TestAttribute1Example") });
attrOverrides.Add(typeof(TestAttribute2.Example), new() { XmlType = new XmlTypeAttribute(typeName: "TestAttribute2Example") });

var serializer = new XmlSerializer(typeof(Tag), attrOverrides);

Tag tag = (Tag)serializer.Deserialize(new StringReader(_xml));

Console.WriteLine(tag.Example2);
}
Hey this works !!! so thats how overrides works, thanks @V.EINA Jaken :)
JakenVeina
JakenVeina14mo ago
sweet
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server