2

I am having trouble when using List objects:

[DataContract]
public class Recipe
{
        [DataMember(Name="Allergies")]
        public List<AllergyModel> Allergies { get; set; }
}

[DataContract]    
public class AllergyModel
{
    public string Allergy { get; set; }
}

How do I make the XML produced not include the AllergyModel node? When I come to read the Recipe parameter, the Allergies list property is null because in the original XML the structure does not have the AllergyModel node.

<Allergies>
    <a:AllergyModel>
        <a:Allergy>nuts</a:Allergy>
    </a:AllergyModel>
    <a:AllergyModel>
        <a:Allergy>wheat</a:Allergy>
    </a:AllergyModel>
</Allergies>
KevinUK
  • 5,053
  • 5
  • 33
  • 49

2 Answers2

3

Try to use custom collection instead:

[CollectionDataContract(Name = "Allergies", ItemName = "Allergy")]
public class AllergyList : List<string>
{
    ...
}

Use this collection instead of List<AllergyModel>. The important is that you can pass the name of the item in the custom collection.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 1
    Thanks, my structure is now nutswheat But when I inspect public XElement SubmitRecipe(Recipe recipe) recipe.Allergies has a count of 0, any ideas? My sample is this (I am receiving simple data types, just not collections). nuts wheat – KevinUK Apr 19 '11 at 13:34
  • I'm not sure I understand the current problem. – Ladislav Mrnka Apr 21 '11 at 21:01
1

You can use MessageContract attribute instead and set the IsWrapped parameter to false

Dipti Mehta
  • 537
  • 3
  • 13