I am attempting to deserialize some XML which I am not responsible for producing. It has a monolithic node and various branches for several modules. The problem is each module may have similar sub nodes that have different nodes and attributes but share the same name. These similar nodes are not namespaced. In abstract it will look something like this as the target type.
<Root>
<Module1>
<Node SomeAttribute="123" />
</Module1>
<Module2>
<Node SomeOtherAttribute="Something" />
</Module2>
</root>
I have seem various suggestions to annotated my pocos with a namespace to avoid the resulting exception when I try to construct a XmlSerializer
using the Root
type that has both Module1
and Module2
as members.
System.InvalidOperationException : Types 'Root.Module1.Item1' and 'Root.Module1.Item2' both use the XML type name, 'Item', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type.
I think if using System.Text.Json
I wouldn't have this problem as the type is decided by the poco class structure not my the name of the node being deserialized.
Is there a way to deserialize this object in it's monolithic form, perhaps by annotating the Module1.Node
and Module1.Node
poco class with decorators?
I couldn't find the relevant decorators when I tried. I did succeed in stopping the XmlSerializer
constructor exception but it stopped recognising the Node
types and was unable to deserialize either.
My next step will to make separate XmlSerializer
instances for each Module and try and see if I can do away with the Root
object which felt inefficient anyway.
Here is an example of the setup in fiddle: https://dotnetfiddle.net/0twN0O