Reputation: 9973
I have a basic WCF service that takes some xml. Some of the xml is a list like so:
<Root>
<Products>
<Product>
<SKU>1234</SKU>
<Price>2533</Price>
<ProductName>Brown Shows</ProductName>
<Quantity>1</Quantity>
</Product>
<Product>
<SKU>345345</SKU>
<Price>2345</Price>
<ProductName>Red Shows</ProductName>
<Quantity>1</Quantity>
</Product>
</Products>
</Root>
In my class that this gets stored into I have:
[DataMember(Name = "Products", Order = 4, IsRequired = false, EmitDefaultValue = false)]
public List<Product> products;
Then in my Product class I have the SKU, Price, ProductName, and Quantity. Other non list items in my class are being set, but it doesnt appear as if the xml is populating my list. Am I missing something?
Heres my Product class
public class Product
{
[DataMember(Name = "SKU", Order = 0)]
public string sku;
// for the request
[DataMember(Name = "Price", Order = 1, IsRequired = false, EmitDefaultValue = false)]
public int price;
[DataMember(Name = "ProductName", Order = 2, IsRequired = false, EmitDefaultValue = false)]
public string productName;
[DataMember(Name = "Quantity", Order = 3, IsRequired = false, EmitDefaultValue = false)]
public int quantity;
// for the response
[DataMember(Name = "Available", Order = 1, IsRequired = false, EmitDefaultValue = false)]
public string available;
}
Upvotes: 3
Views: 2504
Reputation: 1062494
If you have specific xml, DataContractSerializer
may be a poor choice - it isn't designed to give you control. I suspect you might need [XmlSerializerFormat]
on the service if you expect a specific xml format. In this case, some [XmlArray]
/ [XmlArrayItem]
should give that format. Something like (with [XmlSerializerFormat]
on the service-contract):
[XmlRoot("Root")]
public class MyRoot
{
[XmlArray("Products"), XmlArrayItem("Product")]
public List<Product> Products {get;set;}
}
public class Product
{
[XmlElement("SKU")]
public string Sku {get;set;}
public int Price {get;set;}
public string ProductName {get;set;}
public int Quantity {get;set;}
}
Upvotes: 2