Dream
Dream

Reputation: 11

XML string array deserialize issue

I am using Visual Studio 2013. I have created a ASP.Net Web API project and I have added a controller, model and other related classes. Please find below my class details

Products class

public partial class Products{

   [XmlArray("Product")]      // Tried this also XmlArray(ElementName = "Product")
   [XmlArrayItem("productName")]
   public List<string> productName { get; set; }
}

In other class, I am initializing the Products class object as mentioned below

public Products Get()
{
    Products objProducts = new Products
    {
       productName = new List<string>
       {
                    "P1",
                    "P2",
                    "P3"
        };     
    };
    return objProducts;
}

Whenever I execute the url (http://localhost/service/api/Products) on browser I got below XML

<Products>
    <Product>
        <string>P1</string>
        <string>P2</string>
        <string>P3</string>
    </Product>
</Products>

I should like it if I could get this:

<Products>
    <Product>
        <productName>P1</productName>
        <productName>P2</productName>                       
        <productName>P3</productName>
    </Product>
<Products>

Upvotes: 0

Views: 200

Answers (2)

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22094

Problem is with two nested tags - there is nothing in POC resembling that hierarchy. You can get away if you reorganize your POC classes.

There are two ways how web api serialize output into xml. See https://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization for reference.

One is using DataContractSerializer (default) another is using XmlSerializer

// configure web api to use XmlSerializer
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

Now if you're using XmlSerializer:

public class Products
{
    [XmlElement("Product")]
    public ProductNameList Product { get; set; }
}

public class ProductNameList
{
    [XmlElement("productName")]
    public List<string> ProductName { get; set; }
}

Usage:

var serializer = new XmlSerializer(typeof(Products));
var stream = new MemoryStream();
serializer.Serialize(stream, new Products
{
  Product = new ProductNameList { ProductName = new List<string> { "aa", "bb" } }
});
var result = Encoding.ASCII.GetString(stream.ToArray());

Dumps:

<?xml version="1.0"?>
<Products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Product>
    <productName>aa</productName>
    <productName>bb</productName>
  </Product>
</Products>

If you're using DataContractSerializer:

public class Products
{
    public ProductNameList Product { get; set; }
}

[CollectionDataContract(ItemName = "productName")]
public class ProductNameList : List<string>
{
}    

Please note all [Xml...] attributes are ignored when you're using DataContractSerializer - your example is using them so I was under assumption that you use XmlSerializer.

Upvotes: 1

Imad
Imad

Reputation: 7490

Try to use [XmlElement("productName")]

public partial class Products{

   [XmlArray("Product")] 
   [XmlElement("productName")]
   public List<string> productName { get; set; }
}

Upvotes: 0

Related Questions