WCF XML and List Serialization

I have a basic WCF service that spans multiple xml. Some of the xml is a list:

<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 persists, I:

[DataMember(Name = "Products", Order = 4, IsRequired = false, EmitDefaultValue = false)]
public List<Product> products;

Then in my Product class, I have SKU, Price, ProductName, and Quantity. Other elements from the list are not set in my class, but it does not look as if hm filled my list. Did I miss 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;
}
+3
source share
2 answers

Your class Producthas an attribute[DataContract]

+2
source

xml, DataContractSerializer - . , [XmlSerializerFormat] , XML. [XmlArray]/[XmlArrayItem] . - ( [XmlSerializerFormat] -):

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

Source: https://habr.com/ru/post/1725660/


All Articles