views:

289

answers:

2

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;
}
+2  A: 

Does your Product class have a [DataContract] attribute??

marc_s
No, but my main class does with the name set to "Root". I tried setting a DataContract on Product as well but it didnt solve the problem. After I call my function I output the size of my Products list and it outputs 0
Chris Klepeis
The communication is there... I have some simple variables in my main receiving class and when I output them it returns as expected
Chris Klepeis
Apparently I had to set the Namespace = "" for the DataContract on my Product class. This fixed it. Thanks!
Chris Klepeis
+2  A: 

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;}
}
Marc Gravell
I can receive up to 5 different request types, each having a distinct xml format... so I don't think this would work for me. I've tried using XmlRoot before and the response output did not properly set the root name for some reason. This may be because I'm using the WCF REST starter kit?
Chris Klepeis