I cant get the following XML deserialized because of the "type" attribute.
What I've tried so far:
The XML I want to deserialize:
<foundEntities>
<staticGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns1:StaticGroupType">
<number>10000</number>
<name>Gruppe A</name>
</staticGroup>
</foundEntities>
The Class:
[XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "foundEntities")]
public class FoundEntities
{
[XmlElement(ElementName = "staticGroup", Namespace = "")]
public StaticGroup staticGroup { get; set; }
}
[XmlRoot(ElementName = "staticGroup")]
[XmlInclude(typeof(StaticGroupType))]
public class StaticGroup
{
[XmlElement(ElementName = "number")]
public string number { get; set; }
[XmlElement(ElementName = "name")]
public string name { get; set; }
}
public class StaticGroupType : StaticGroup
{
}
Probably I am blind but what do I miss here? Any hint is highly appreciated.
I have generated the classes using https://xmltocsharp.azurewebsites.net/ and it gave me the following output:
[XmlRoot(ElementName = "staticGroup")]
public class FoundStaticGroup
{
[XmlElement(ElementName = "number")]
public string Number { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "foundEntities")]
public class FoundGroupEntities
{
[XmlElement(ElementName = "staticGroup")]
public StaticGroup StaticGroup { get; set; }
}
But what I get now is an error as already had a few times before:
"The specified type was not recognized: name='StaticGroupType', namespace=' http://com.f24.soap.fwi.schema ', at ."
I have posted a working solution here:
I'm not sure if there are external schemas but I had to remove xsi:type="ns1:StaticGroupType"
to get it to work. Your XML won't parse in most readers unless there is a definition for that namespace in the XML or a schema. Not knowing what your backend service is, I can't recommend how to resolve this.
The biggest issue is you had both your classes marked with the XmlRootAtribute
code attribute. Only one class can represent the root in your example. I also removed unnecessary attributes like Namespace
that don't have any impact since you are using the default namespace.
Update
The issue here is with the incoming XML it is not complete, it needs a schema reference to be able to resolve the StaticGroup
type. You will have to contact the people on the backend to fix this, or strip that part of the XML manually. Correct XML would be as shown below:
<foundEntities
xsi:schemaLocation="http://com.f24.soap.fwi.schema (url to back-end schema here)">
<staticGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns1:StaticGroupType">
<number>10000</number>
<name>Gruppe A</name>
</staticGroup>
</foundEntities>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.