Home > front end >  C# XML to POCO - same "xsi:type" cannot be used by different elements
C# XML to POCO - same "xsi:type" cannot be used by different elements

Time:09-12

I have the following XML that needs to be deserialized into POCOs. Let us assume that for the time being the XML cannot be modified:

<?xml version="1.0" encoding="UTF-8"?>
<query:QueryRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xmime="http://www.w3.org/2005/05/xmlmime" 
    xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0"
    xmlns:query="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xml="http://www.w3.org/XML/1998/namespace"
    id="4ffb5281-179d-4578-adf2-39fd13ccc797"> 
    
    <rim:Slot name="SpecificationIdentifier">
        <rim:SlotValue xsi:type="rim:StringValueType">
            <rim:Value>sfts-edm:v1.0</rim:Value>
        </rim:SlotValue>
    </rim:Slot>
    <rim:Slot name="IssueDateTime">
        <rim:SlotValue xsi:type="rim:DateTimeValueType">
            <rim:Value>2022-05-19T17:08:10.872Z</rim:Value>
        </rim:SlotValue>
    </rim:Slot>
    <rim:Slot name="Procedure">
        <rim:SlotValue xsi:type="rim:InternationalStringValueType">
            <rim:Value>
                <rim:LocalizedString xml:lang="en"
                    value="Requesting a certificate"/>
            </rim:Value>
        </rim:SlotValue>
    </rim:Slot>
    <rim:Slot name="PossibilityForPreview">
        <rim:SlotValue xsi:type="rim:BooleanValueType">
            <rim:Value>false</rim:Value>
        </rim:SlotValue>
    </rim:Slot> 
    <rim:Slot name="ExplicitRequestGiven">
        <rim:SlotValue xsi:type="rim:BooleanValueType">
            <rim:Value>true</rim:Value>
        </rim:SlotValue>
    </rim:Slot>
    <rim:Slot name="Requirements">
        <rim:SlotValue xsi:type="rim:CollectionValueType" 
            collectionType="urn:oasis:names:tc:ebxml-regrep:CollectionType:Set">
            <rim:Element xsi:type="rim:AnyValueType">
                <rim:Requirement>
                    <Identifier>315cfd75-6605-49c4-b0fe-799833b41099</Identifier>
                    <Name lang="en">Proof of Birth</Name>
                </rim:Requirement>
            </rim:Element>
        </rim:SlotValue>
    </rim:Slot>
    <rim:Slot name="EvidenceProvider">
        <rim:SlotValue xsi:type="rim:AnyValueType">
            <Agent>
                <Identifier schemeID="9930">DE73524311</Identifier>
                <Name>Civil Registration Office Berlin I</Name>
            </Agent>
        </rim:SlotValue>
    </rim:Slot>
    <query:ResponseOption returnType="LeafClassWithRepositoryItem"/>
    <query:Query queryDefinition="DocumentQuery">
        <rim:Slot name="NaturalPerson">
            <rim:SlotValue xsi:type="rim:AnyValueType">
                <Person>
                    <LevelOfAssurance>High</LevelOfAssurance>
                    <Identifier schemeID="eidas">DK/DE/123456</Identifier>
                    <FamilyName>Smith</FamilyName>
                    <GivenName>John</GivenName>
                    <DateOfBirth>1970-03-01</DateOfBirth>
                    <PlaceOfBirth>Hamburg, Germany</PlaceOfBirth>
                    <CurrentAddress>
                        <FullAddress>Dieter Wellhausen 71</FullAddress>
                        <AdminUnitLevel1>DE</AdminUnitLevel1>
                    </CurrentAddress>
                    <Gender>Male</Gender>
                </Person>
            </rim:SlotValue>
        </rim:Slot>
        <rim:Slot name="EvidenceRequest">
            <rim:SlotValue xsi:type="rim:AnyValueType">
                <DataServiceEvidenceType xmlns="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0">
                    <Identifier>2af27699-f131-4411-8fdb-9e8cd4e8bded</Identifier>
                    <EvidenceTypeClassification>Certificate</EvidenceTypeClassification>
                    <Title lang="en">Certificate of Birth</Title>
                </DataServiceEvidenceType>
            </rim:SlotValue>
        </rim:Slot>
    </query:Query>
</query:QueryRequest>

Using the above XML, I created the following classes using XMLToCSharp:

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace TestRequest2
{
    [XmlRoot(ElementName="SlotValue", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class SlotValue {
        [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
        [XmlElement(ElementName="Value", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Value Value { get; set; }
        [XmlElement(ElementName="Element", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Element Element { get; set; }
        [XmlAttribute(AttributeName="collectionType")]
        public string CollectionType { get; set; }
        [XmlElement(ElementName="Agent")]
        public Agent Agent { get; set; }
        [XmlElement(ElementName="Person")]
        public Person Person { get; set; }
        [XmlElement(ElementName="DataServiceEvidenceType", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public DataServiceEvidenceType DataServiceEvidenceType { get; set; }
    }

    [XmlRoot(ElementName="Slot", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Slot {
        [XmlElement(ElementName="SlotValue", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public SlotValue SlotValue { get; set; }
        [XmlAttribute(AttributeName="name")]
        public string Name { get; set; }
    }

    [XmlRoot(ElementName="LocalizedString", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class LocalizedString {
        [XmlAttribute(AttributeName="lang", Namespace="http://www.w3.org/XML/1998/namespace")]
        public string Lang { get; set; }
        [XmlAttribute(AttributeName="value")]
        public string Value { get; set; }
    }

    [XmlRoot(ElementName="Value", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Value {
        [XmlElement(ElementName="LocalizedString", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public LocalizedString LocalizedString { get; set; }
    }

    [XmlRoot(ElementName="Name")]
    public class Name {
        [XmlAttribute(AttributeName="lang")]
        public string Lang { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName="Requirement", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Requirement {
        [XmlElement(ElementName="Identifier")]
        public string Identifier { get; set; }
        [XmlElement(ElementName="Name")]
        public Name Name { get; set; }
    }

    [XmlRoot(ElementName="Element", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Element {
        [XmlElement(ElementName="Requirement", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Requirement Requirement { get; set; }
        [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
    }

    [XmlRoot(ElementName="Identifier")]
    public class Identifier {
        [XmlAttribute(AttributeName="schemeID")]
        public string SchemeID { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName="Agent")]
    public class Agent {
        [XmlElement(ElementName="Identifier")]
        public Identifier Identifier { get; set; }
        [XmlElement(ElementName="Name")]
        public string Name { get; set; }
    }

    [XmlRoot(ElementName="ResponseOption", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
    public class ResponseOption {
        [XmlAttribute(AttributeName="returnType")]
        public string ReturnType { get; set; }
    }

    [XmlRoot(ElementName="CurrentAddress")]
    public class CurrentAddress {
        [XmlElement(ElementName="FullAddress")]
        public string FullAddress { get; set; }
        [XmlElement(ElementName="AdminUnitLevel1")]
        public string AdminUnitLevel1 { get; set; }
    }

    [XmlRoot(ElementName="Person")]
    public class Person {
        [XmlElement(ElementName="LevelOfAssurance")]
        public string LevelOfAssurance { get; set; }
        [XmlElement(ElementName="Identifier")]
        public Identifier Identifier { get; set; }
        [XmlElement(ElementName="FamilyName")]
        public string FamilyName { get; set; }
        [XmlElement(ElementName="GivenName")]
        public string GivenName { get; set; }
        [XmlElement(ElementName="DateOfBirth")]
        public string DateOfBirth { get; set; }
        [XmlElement(ElementName="PlaceOfBirth")]
        public string PlaceOfBirth { get; set; }
        [XmlElement(ElementName="CurrentAddress")]
        public CurrentAddress CurrentAddress { get; set; }
        [XmlElement(ElementName="Gender")]
        public string Gender { get; set; }
    }

    [XmlRoot(ElementName="Title", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Title {
        [XmlAttribute(AttributeName="lang")]
        public string Lang { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName="DataServiceEvidenceType", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class DataServiceEvidenceType {
        [XmlElement(ElementName="Identifier", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public string Identifier2 { get; set; }
        [XmlElement(ElementName="EvidenceTypeClassification", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public string EvidenceTypeClassification { get; set; }
        [XmlElement(ElementName="Title", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Title Title { get; set; }
        [XmlAttribute(AttributeName="xmlns")]
        public string Xmlns { get; set; }
    }

    [XmlRoot(ElementName="Query", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
    public class Query {
        [XmlElement(ElementName="Slot", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public List<Slot> Slot { get; set; }
        [XmlAttribute(AttributeName="queryDefinition")]
        public string QueryDefinition { get; set; }
    }

    [XmlRoot(ElementName="QueryRequest", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
    public class QueryRequest {
        [XmlElement(ElementName="Slot", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public List<Slot> Slot { get; set; }
        [XmlElement(ElementName="ResponseOption", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
        public ResponseOption ResponseOption { get; set; }
        [XmlElement(ElementName="Query", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
        public Query Query { get; set; }
        [XmlAttribute(AttributeName="query", Namespace="http://www.w3.org/2000/xmlns/")]
        public string _Query { get; set; }
        [XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName="xmime", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Xmime { get; set; }
        [XmlAttribute(AttributeName="rim", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Rim { get; set; }
        [XmlAttribute(AttributeName="xlink", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Xlink { get; set; }
        [XmlAttribute(AttributeName="xml", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Xml { get; set; }
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
    }

}

When I attempted to deserialize, the following error was thrown:

Unhandled exception. System.InvalidOperationException: There is an error in XML document (11, 4).
 ---> System.InvalidOperationException: The specified type was not recognized: name='StringValueType', namespace='urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0', at <SlotValue xmlns='urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0'>.

After some research, I came across this StackOverflow post which recommended that all the possible types of xsi:type should be defined as subclasses of SlotValue and included using the [XmlInclude] attribute. This seemed to bring some progress. I modified the class SlotValue to look like this:

[XmlRoot(ElementName="SlotValue", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
[XmlInclude(typeof(StringValueType))]
[XmlInclude(typeof(DateTimeValueType))]
[XmlInclude(typeof(InternationalStringValueType))]
[XmlInclude(typeof(BooleanValueType))]
[XmlInclude(typeof(CollectionValueType))]
[XmlInclude(typeof(AnyValueType))]
public class SlotValue {
    [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
    public string Type { get; set; }
    [XmlElement(ElementName="Value", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public Value Value { get; set; }
    [XmlElement(ElementName="Element", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public Element Element { get; set; }
    [XmlAttribute(AttributeName="collectionType")]
    public string CollectionType { get; set; }
    [XmlElement(ElementName="Agent")]
    public Agent Agent { get; set; }
    [XmlElement(ElementName="Person")]
    public Person Person { get; set; }
    [XmlElement(ElementName="DataServiceEvidenceType", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public DataServiceEvidenceType DataServiceEvidenceType { get; set; }

}
[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "StringValueType")]
public class StringValueType : SlotValue {}

[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "DateTimeValueType")]
public class DateTimeValueType : SlotValue {}

[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "InternationalStringValueType")]
public class InternationalStringValueType : SlotValue {}

[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "BooleanValueType")]
public class BooleanValueType : SlotValue {}

[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "CollectionValueType")]
public class CollectionValueType : SlotValue {}

[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "AnyValueType")]
public class AnyValueType : SlotValue {}

However, in doing so, the same exception would be thrown, this time on another line belonging to another element Element:

    Unhandled exception. System.InvalidOperationException: There is an error in XML document (41, 5).
 ---> System.InvalidOperationException: The specified type was not recognized: name='AnyValueType', namespace='urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0', at <Element xmlns='urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0'>.

It seems that the above list of types is expected to be used by more than one XML Element. In this case, AnyValueType seems to be shared between SlotValue and Element. So far I have not been able to find a workaround for this.

My most recent attempt has included placing the [XmlType] attribute on top of Element, like so:

[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "AnyValueType")]
[XmlRoot(ElementName="Element", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
[XmlInclude(typeof(AnyValueType))]
public class Element {

But this would conflict with the attributes placed on SlotValue as the following exception gets thrown if I were to do that:

 ---> System.InvalidOperationException: Types 'TestRequest.AnyValueType' and 'TestRequest.Element' both use the XML type name, 'AnyValueType', from namespace 'urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0'. Use XML attributes to
 specify a unique XML name and/or namespace for the type.

CodePudding user response:

The xsi:type attribute indicates an inherited class. XML Serialize is not easy to debug. I commented out the sections of your classes to isolate issue. Then fixed issue and the added the commented code back into the classes. Here are the changes I made

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace ConsoleApplication40
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(QueryRequest));
            QueryRequest request = (QueryRequest)serializer.Deserialize(reader);

        }
    }
    [XmlInclude(typeof(AnyValueType))]
    [XmlInclude(typeof(StringValueType))]
    [XmlInclude(typeof(InternationalStringValueType))]
    [XmlInclude(typeof(DateTimeValueType))]
    [XmlInclude(typeof(BooleanValueType))]
    [XmlInclude(typeof(CollectionValueType))]
    [XmlInclude(typeof(Element))]
    
    public class SlotValue
    {
        [XmlAttribute("name")]
        public string name { get; set; }
        [XmlElement(ElementName = "Agent")]
        public Agent Agent { get; set; }
        [XmlElement(ElementName = "Person")]
        public Person Person { get; set; }
        [XmlElement(ElementName = "DataServiceEvidenceType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public DataServiceEvidenceType DataServiceEvidenceType { get; set; }
    }
  
    [XmlRoot(ElementName = "AnyValueType", Namespace  = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class AnyValueType : SlotValue
    {
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public object Value { get; set; }

    }
    [XmlRoot(ElementName = "DateTimeValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class DateTimeValueType : SlotValue
    {
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public DateTime Value { get; set; }
    }
    [XmlRoot(ElementName = "StringValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class StringValueType : SlotValue
    {
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Value  Value { get; set; }

    }
    [XmlRoot(ElementName = "InternationalStringValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class InternationalStringValueType : SlotValue
    {
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Value  Value { get; set; }
 
    }

    [XmlRoot(ElementName = "BooleanValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class BooleanValueType : SlotValue
    {
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Boolean Value { get; set; }
 
    }
    [XmlRoot(ElementName = "CollectionValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class CollectionValueType : SlotValue
    {
        //[XmlElement(ElementName = "Element", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        //public Element Element { get; set; }
 
    }
    [XmlRoot(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Slot
    {
        [XmlElement(ElementName = "SlotValue", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public SlotValue SlotValue { get; set; }
        [XmlAttribute(AttributeName = "name")]
        public string Name { get; set; }
    }

    [XmlRoot(ElementName = "LocalizedString", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class LocalizedString
    {
        [XmlAttribute(AttributeName = "lang", Namespace = "http://www.w3.org/XML/1998/namespace")]
        public string Lang { get; set; }
        [XmlAttribute(AttributeName = "value")]
        public string Value { get; set; }
    }

    [XmlRoot(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Value
    {
        [XmlElement(ElementName = "LocalizedString", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public LocalizedString LocalizedString { get; set; }
    }

    [XmlRoot(ElementName = "Name")]
    public class Name
    {
        [XmlAttribute(AttributeName = "lang")]
        public string Lang { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "Requirement", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Requirement
    {
        [XmlElement(ElementName = "Identifier")]
        public string Identifier { get; set; }
        [XmlElement(ElementName = "Name")]
        public Name Name { get; set; }
    }

    [XmlInclude(typeof(AnyValueTypeElement))]
    [XmlRoot(ElementName = "Element", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Element
    {
        [XmlElement(ElementName = "Requirement", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Requirement Requirement { get; set; }
        [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
    }
    [XmlRoot(ElementName = "AnyValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class AnyValueTypeElement : Element
    {
    }
   

    [XmlRoot(ElementName = "Identifier")]
    public class Identifier
    {
        [XmlAttribute(AttributeName = "schemeID")]
        public string SchemeID { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "Agent")]
    public class Agent
    {
        [XmlElement(ElementName = "Identifier")]
        public Identifier Identifier { get; set; }
        [XmlElement(ElementName = "Name")]
        public string Name { get; set; }
    }

    [XmlRoot(ElementName = "ResponseOption", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
    public class ResponseOption
    {
        [XmlAttribute(AttributeName = "returnType")]
        public string ReturnType { get; set; }
    }

    [XmlRoot(ElementName = "CurrentAddress")]
    public class CurrentAddress
    {
        [XmlElement(ElementName = "FullAddress")]
        public string FullAddress { get; set; }
        [XmlElement(ElementName = "AdminUnitLevel1")]
        public string AdminUnitLevel1 { get; set; }
    }

    [XmlRoot(ElementName = "Person")]
    public class Person
    {
        [XmlElement(ElementName = "LevelOfAssurance")]
        public string LevelOfAssurance { get; set; }
        [XmlElement(ElementName = "Identifier")]
        public Identifier Identifier { get; set; }
        [XmlElement(ElementName = "FamilyName")]
        public string FamilyName { get; set; }
        [XmlElement(ElementName = "GivenName")]
        public string GivenName { get; set; }
        [XmlElement(ElementName = "DateOfBirth")]
        public string DateOfBirth { get; set; }
        [XmlElement(ElementName = "PlaceOfBirth")]
        public string PlaceOfBirth { get; set; }
        [XmlElement(ElementName = "CurrentAddress")]
        public CurrentAddress CurrentAddress { get; set; }
        [XmlElement(ElementName = "Gender")]
        public string Gender { get; set; }
    }

    [XmlRoot(ElementName = "Title", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Title
    {
        [XmlAttribute(AttributeName = "lang")]
        public string Lang { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "DataServiceEvidenceType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class DataServiceEvidenceType
    {
        [XmlElement(ElementName = "Identifier", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public string Identifier2 { get; set; }
        [XmlElement(ElementName = "EvidenceTypeClassification", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public string EvidenceTypeClassification { get; set; }
        [XmlElement(ElementName = "Title", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Title Title { get; set; }
     }

    [XmlRoot(ElementName = "Query", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
    public class Query
    {
        [XmlElement(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public List<Slot> Slot { get; set; }
        [XmlAttribute(AttributeName = "queryDefinition")]
        public string QueryDefinition { get; set; }
    }

    [XmlRoot(ElementName = "QueryRequest", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
    public class QueryRequest
    {
        [XmlElement(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public List<Slot> Slot { get; set; }
        [XmlElement(ElementName = "ResponseOption", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
        public ResponseOption ResponseOption { get; set; }
        [XmlElement(ElementName = "Query", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
        public Query Query { get; set; }
        [XmlAttribute(AttributeName = "query", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string _Query { get; set; }
        [XmlAttribute(AttributeName = "id")]
        public string Id { get; set; }
    }
}

CodePudding user response:

Thank you @jdweng, I managed to find a solution to the problem.

Basically, I removed class Element and merged its properties (in this case only one - I removed type since it was redundant) into AnyValueType. Then, I simply replaced the call to Element in CollectionValueType to AnyValueType. To take @jdweng's example, it should look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace ConsoleApplication40
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(QueryRequest));
            QueryRequest request = (QueryRequest)serializer.Deserialize(reader);

        }
    }
    [XmlInclude(typeof(AnyValueType))]
    [XmlInclude(typeof(StringValueType))]
    [XmlInclude(typeof(InternationalStringValueType))]
    [XmlInclude(typeof(DateTimeValueType))]
    [XmlInclude(typeof(BooleanValueType))]
    [XmlInclude(typeof(CollectionValueType))]
    [XmlInclude(typeof(Element))]
    
    public class SlotValue
    {
        [XmlAttribute("name")]
        public string name { get; set; }
        [XmlElement(ElementName = "Agent")]
        public Agent Agent { get; set; }
        [XmlElement(ElementName = "Person")]
        public Person Person { get; set; }
        [XmlElement(ElementName = "DataServiceEvidenceType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public DataServiceEvidenceType DataServiceEvidenceType { get; set; }
    }
  
    [XmlRoot(ElementName = "AnyValueType", Namespace  = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class AnyValueType : SlotValue
    {
        [XmlElement(ElementName = "Requirement", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Requirement Requirement { get; set; }
        
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public object Value { get; set; }

    }
    [XmlRoot(ElementName = "DateTimeValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class DateTimeValueType : SlotValue
    {
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public DateTime Value { get; set; }
    }
    [XmlRoot(ElementName = "StringValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class StringValueType : SlotValue
    {
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Value  Value { get; set; }

    }
    [XmlRoot(ElementName = "InternationalStringValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class InternationalStringValueType : SlotValue
    {
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Value  Value { get; set; }
 
    }

    [XmlRoot(ElementName = "BooleanValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class BooleanValueType : SlotValue
    {
        [XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Boolean Value { get; set; }
 
    }
    [XmlRoot(ElementName = "CollectionValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class CollectionValueType : SlotValue
    {
        [XmlElement("Element", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public AnyValueType Element { get; set; }
    }
    [XmlRoot(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Slot
    {
        [XmlElement(ElementName = "SlotValue", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public SlotValue SlotValue { get; set; }
        [XmlAttribute(AttributeName = "name")]
        public string Name { get; set; }
    }

    [XmlRoot(ElementName = "LocalizedString", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class LocalizedString
    {
        [XmlAttribute(AttributeName = "lang", Namespace = "http://www.w3.org/XML/1998/namespace")]
        public string Lang { get; set; }
        [XmlAttribute(AttributeName = "value")]
        public string Value { get; set; }
    }

    [XmlRoot(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Value
    {
        [XmlElement(ElementName = "LocalizedString", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public LocalizedString LocalizedString { get; set; }
    }

    [XmlRoot(ElementName = "Name")]
    public class Name
    {
        [XmlAttribute(AttributeName = "lang")]
        public string Lang { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "Requirement", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Requirement
    {
        [XmlElement(ElementName = "Identifier")]
        public string Identifier { get; set; }
        [XmlElement(ElementName = "Name")]
        public Name Name { get; set; }
    }

    [XmlRoot(ElementName = "Identifier")]
    public class Identifier
    {
        [XmlAttribute(AttributeName = "schemeID")]
        public string SchemeID { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "Agent")]
    public class Agent
    {
        [XmlElement(ElementName = "Identifier")]
        public Identifier Identifier { get; set; }
        [XmlElement(ElementName = "Name")]
        public string Name { get; set; }
    }

    [XmlRoot(ElementName = "ResponseOption", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
    public class ResponseOption
    {
        [XmlAttribute(AttributeName = "returnType")]
        public string ReturnType { get; set; }
    }

    [XmlRoot(ElementName = "CurrentAddress")]
    public class CurrentAddress
    {
        [XmlElement(ElementName = "FullAddress")]
        public string FullAddress { get; set; }
        [XmlElement(ElementName = "AdminUnitLevel1")]
        public string AdminUnitLevel1 { get; set; }
    }

    [XmlRoot(ElementName = "Person")]
    public class Person
    {
        [XmlElement(ElementName = "LevelOfAssurance")]
        public string LevelOfAssurance { get; set; }
        [XmlElement(ElementName = "Identifier")]
        public Identifier Identifier { get; set; }
        [XmlElement(ElementName = "FamilyName")]
        public string FamilyName { get; set; }
        [XmlElement(ElementName = "GivenName")]
        public string GivenName { get; set; }
        [XmlElement(ElementName = "DateOfBirth")]
        public string DateOfBirth { get; set; }
        [XmlElement(ElementName = "PlaceOfBirth")]
        public string PlaceOfBirth { get; set; }
        [XmlElement(ElementName = "CurrentAddress")]
        public CurrentAddress CurrentAddress { get; set; }
        [XmlElement(ElementName = "Gender")]
        public string Gender { get; set; }
    }

    [XmlRoot(ElementName = "Title", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class Title
    {
        [XmlAttribute(AttributeName = "lang")]
        public string Lang { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "DataServiceEvidenceType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
    public class DataServiceEvidenceType
    {
        [XmlElement(ElementName = "Identifier", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public string Identifier2 { get; set; }
        [XmlElement(ElementName = "EvidenceTypeClassification", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public string EvidenceTypeClassification { get; set; }
        [XmlElement(ElementName = "Title", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public Title Title { get; set; }
     }

    [XmlRoot(ElementName = "Query", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
    public class Query
    {
        [XmlElement(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public List<Slot> Slot { get; set; }
        [XmlAttribute(AttributeName = "queryDefinition")]
        public string QueryDefinition { get; set; }
    }

    [XmlRoot(ElementName = "QueryRequest", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
    public class QueryRequest
    {
        [XmlElement(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
        public List<Slot> Slot { get; set; }
        [XmlElement(ElementName = "ResponseOption", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
        public ResponseOption ResponseOption { get; set; }
        [XmlElement(ElementName = "Query", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
        public Query Query { get; set; }
        [XmlAttribute(AttributeName = "query", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string _Query { get; set; }
        [XmlAttribute(AttributeName = "id")]
        public string Id { get; set; }
    }
}
  • Related