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?