KevinUK
KevinUK

Reputation: 5153

WCF XML structure - how to remove wrapper nodes?

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>

Upvotes: 2

Views: 1277

Answers (2)

Dipti Mehta
Dipti Mehta

Reputation: 547

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

Upvotes: 1

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364389

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.

Upvotes: 3

Related Questions