We have a legacy system that needs to be fed (XML) data in a most unstructured format. Is the following even possible with the .NET DataContractSerializer
?
Given the following DataContracts
[CollectionDataContract(Name = "Options", ItemName = "Option")]
public class OptionItemCollection : List<OptionItem>
{
[DataMember(Name = "Name")]
public string Name { get; set; }
public OptionItemCollection()
{
}
public OptionItemCollection(IEnumerable<OptionItem> items) : base(items)
{
}
}
[DataContract(Name = "Option")]
public class OptionItem
{
[DataMember]
public string Text { get; set; }
[DataMember]
public string Value { get; set; }
}
Is it possible to serialize this collection directly into the following XML representation:
<Options>
<Name>Juices</Name>
<Option Value="1">Orange Juice</Option>
<Option Value="2">Pineapple</Option>
<Option Value="3">Fruit Punch</Option>
</Options>
NOTE: This is exactly how the legacy system expects the data to be submitted.
Or Even:
<Options>
<Name>Juices</Name>
<Option><Value>1</Value><Text>Orange Juice</Text></Option>
<Option><Value>2</Value><Text>Pineapple</Text></Option>
<Option><Value>3</Value><Text>Fruit Punch</Text></Option>
</Options>
Also NOTE that the emphasis is on the Name and Option element residing within the Options element.