0

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>
1
  • 1
    Can you please reformat your question to make it readable? (without the inline HTML and format the XML as code) Commented Dec 14, 2016 at 13:49

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have updated the code of Products and ProductNameList class. I am intializing the Products class object as mentioned below Products objVersionsResponse = new Products { Product = new ProductList { productName = new List<string> { "v1", "v2" } } }; <Products> <Product> <productName> <string>v1</string> <string>v2</string> </productName> </Product> </Products> Instead of <Products> <Product> <productName>v1</productName> <productName>v2</productName> </Product> </Products>
As I mentioned in my question I have created WebAPI project. When I executing it on a browser like localhost/service/api/Products, it shows be XML like above.
0

Try to use [XmlElement("productName")]

public partial class Products{

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

1 Comment

XmlElement, XmlText, and XmlAnyElement cannot be used in conjunction with XmlAttribute, XmlAnyAttribute, XmlArray, or XmlArrayItem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.