0

The data source I'm using always sends over data with the same parent class (Models in the xml), with xsi:type to determine the actual type of the class. This has been working fine until they started adding a namespace to the xsi:type. Now it won't deserialize no matter what I try.

Here's the XML:

<ModelResource xmlns:ot="http://www.example.com/otSpace">
  <Models xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xsi:type="ot:myChildClass">
    Stuff here
  </Models>
</ModelResource>

The root node

[XmlRoot("ModelResource")]
public class XmlRoot
{
    [XmlElement("Models")]
    public List<BaseObject> Bases { get; set; }
}

The parent class

[XmlInclude(typeof(MyChildClass))]
public abstract class BaseObject
{
}

The child class

[XmlType(TypeName = "myChildClass", Namespace = "http://www.example.com/otSpace")]
public class MyChildClass : BaseObject
{
}

When I deserialize this XML, I wind up with the error:

{"The specified type was not recognized: name='myChildClass', namespace='http://www.example.com/otSpace', at ."}

Thanks for your help.

1 Answer 1

2

These classes work with your example. I've named the classes the same as the element names just to make it easier to follow:

public class ModelResource
{
    public Models Models { get; set; }
}

[XmlInclude(typeof(MyChildClass))]
[XmlRoot(Namespace = "")]
public abstract class Models
{
}

[XmlType("myChildClass", Namespace = "http://www.example.com/otSpace")]
public class MyChildClass : Models
{
    [XmlText]
    public string Value { get; set; }
}

See this fiddle for a working demo.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Setting the parent namespace explicitly to "" is the missing piece. Now I have to deal with the fact that while MyChildClass belongs to that namespace, none of its properties belong to that namespace, so I need to set them all explicitly to "" as well, but that's just busywork.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.