Question

I have this object structure:

[DataContract]
[KnownType(typeof(ChildClassA))]
[KnownType(typeof(ChildClassB))]
public class MainClass
{          
    [DataMember]
    public string PropA { get; set; }       
    [DataMember]
    public IList<IMyInterface> GenericInterfaceList { get; set; }       
    //....
}    
public interface IMyInterface
{
    int Id { get; set; }        
    //....
}    
[DataContract]
public abstract class BaseClass :  IMyInterface
{
    [DataMember]
    public int Id { get; set; }     
    //....
}    
[DataContract]
public class ChildClassA : BaseClass
{
    //....
}    
[DataContract]
public class ChildClassB : BaseClass
{
    //.....
}

I want that DataContractSerializer emits an Xml without a:anyType i:type= for my generic list of interfaces:

<MainClass xmlns="MYNAMESPACE" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <PropA> dfgdsfg gsd</PropA>
    <GenericInterfaceList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <ChildClassA>
            <Id>1</Id>
        </ChildClassA>
        <ChildClassB>
            <Id>2</Id>
        </ChildClassB>
    </GenericInterfaceList>
</MainClass>

instead of:

<MainClass xmlns="MYNAMESPACE" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <PropA> dfgdsfg gsd</PropA>
    <GenericInterfaceList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <a:anyType i:type="ChildClassA">
            <Id>1</Id>
        </a:anyType>
        <a:anyType i:type="ChildClassB">
            <Id>2</Id>
        </a:anyType>
    </GenericInterfaceList>
</MainClass>

I tryed with a custom DataContractResolver but it is not the right tool. Cannot find a way to shrink that Xml.

Was it helpful?

Solution

I could not find a way because there is not.

The DataContractSerializer doesn't allow much control: you can define quite clearly (using this explicit "opt-in" approach) what gets serialized, but you have little or no control over how it gets serialized.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top