0

I defined these classes:

 [DataContract(Name = "cardTemplateDefinition", Namespace = "")]
    public class CardTemplateDefinition
    {
      [DataMember(IsRequired = false, Name = "riskInfoList")]
        public RiskInfoList RiskInfoList { get; set; }
    }

    [DataContract(Namespace = "")]
    public class RiskInfoList
    {
        [DataMember(IsRequired = false, Name = "riskCount")]
        public string RiskCount { get; set; }

        [DataMember(IsRequired = false, Name = "riskInfo")]
        public List<RiskInfo> RiskInfo { get; set; }
    }

    [DataContract(Namespace = "")]
    public class RiskInfo
    {
        [DataMember(IsRequired = false, Name = "riskType")]
        public string RiskType { get; set; }

        [DataMember(IsRequired = false, Name = "riskDescription")]
        public string RiskDescription { get; set; }

        [DataMember(IsRequired = false, Name = "autoRisk")]
        public AutoRiskEntity AutoRisk { get; set; }

        [DataMember(IsRequired = false, Name = "dwellingRisk")]
        public DwellingRiskEntity DwellingRisk { get; set; }
    }

    [DataContract(Namespace = "")]
    public class DwellingRiskEntity
    {
        [DataMember(IsRequired = false, Name = "location")]
        public string Location { get; set; }
    }

    [DataContract(Namespace = "")]
    public class AutoRiskEntity
    {
        [DataMember(IsRequired = false, Name = "vin")]
        public string Vin { get; set; }

        [DataMember(IsRequired = false, Name = "make")]
        public string Make { get; set; }

        [DataMember(IsRequired = false, Name = "model")]
        public string Model { get; set; }

        [DataMember(IsRequired = false, Name = "licensePlate")]
        public string LicensePlate { get; set; }

        [DataMember(IsRequired = false, Name = "year")]
        public string Year { get; set; }
    }

This is creating the following XML

<riskInfoList>
   <riskCount></riskCount>
   <riskInfo>
      <!--Zero or more repetitions:-->
      <riskInfo>
         <autoRisk>
            <licensePlate>?</licensePlate>
             <make>?</make>
            <model>?</model>
            <vin>?</vin>
            <year>?</year>
         </autoRisk>
         <dwellingRisk>
            <location>?</location>
         </dwellingRisk>
         <riskDescription>?</riskDescription>
         <riskType>?</riskType>
      </riskInfo>
   </riskInfo>
</riskInfoList>

But what I want is:

  <riskInfoList>
     <riskCount>?</riskCount>
          <!--Zero or more repetitions:-->
          <riskInfo>
             <autoRisk>
                <licensePlate>?</licensePlate>
                 <make>?</make>
                <model>?</model>
                <vin>?</vin>
                <year>?</year>
             </autoRisk>
             <dwellingRisk>
                <location>?</location>
             </dwellingRisk>
             <riskDescription>?</riskDescription>
             <riskType>?</riskType>
          </riskInfo>
    </riskInfoList>

I want to get rid of the riskInfo duplicate root. I tried to change the field definition by eliminating the RiskInfoList class and change the RiskInfoList field in the carddefinition class to be directly a list of RiskInfo. This rendered the xml as I wanted but then I cannot included the riskCount element becuase it needs to be at the riskinfolist level.

How can I remediate this?

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
Ray
  • 483
  • 4
  • 17
  • Unfortunately `DataContractSerializer` mandates use of an outer element for collections; see [Data Contract Serializer - How to omit the outer element of a collection](https://stackoverflow.com/q/8591045/3744182) and [WCF DataMember List<> without enclosing element](https://stackoverflow.com/q/3267709/3744182) which basically say you have to switch to `XmlSerializer`. – dbc Apr 20 '18 at 21:48

1 Answers1

1

The DataContractSerializer does not support what you're wanting to do, but you can switch to use the XmlSerializer and achieve the outcome you're looking for:

First, update your ServiceContract to also have the XmlSerializerFormat attribute:

[ServiceContract, XmlSerializerFormat]
public interface IService1
{
    [OperationContract]
    CardTemplateDefinition SomeMethod(CardTemplateDefinition composite);

}

Then remove the [DataContract] attributes from your model classes and change the [DataContract] attributes for [XmlElement] attributes:

public class CardTemplateDefinition
{
    [XmlElement("riskInfoList")]      
    public RiskInfoList RiskInfoList { get; set; }
}

public class RiskInfoList 
{
    [XmlElement("riskCount")]
    public string RiskCount { get; set; }

    [XmlElement("riskInfo")]
    public List<RiskInfo> RiskInfo { get; set; }
}

public class RiskInfo
{
    [XmlElement("riskType")]
    public string RiskType { get; set; }

    [XmlElement("riskDescription")]
    public string RiskDescription { get; set; }

    [XmlElement("autoRisk")]
    public AutoRiskEntity AutoRisk { get; set; }

    [XmlElement("dwellingRisk")]
    public DwellingRiskEntity DwellingRisk { get; set; }
}


public class DwellingRiskEntity
{
    [XmlElement("location")]        
    public string Location { get; set; }
}

public class AutoRiskEntity
{
    [XmlElement("vin")]
    public string Vin { get; set; }

    [XmlElement("make")]
    public string Make { get; set; }

    [XmlElement("model")]
    public string Model { get; set; }

    [XmlElement("licensePlate")]
    public string LicensePlate { get; set; }

    [XmlElement("year")]
    public string Year { get; set; }
}
John M. Wright
  • 4,477
  • 1
  • 43
  • 61