Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define xml attributes using web api and model binding

I'm creating an xml feed of products which needs to match the clients scheme exactly.

I'm using web api. I would like the property extractDate to be an attribute. The following code is outputting extractDate as an element not an attribute

    public Feed GetProducts()
    {
             var feed = new Feed()
             {
                 extractDate = "extractDate",
                 incremental = true,
                 name = "name",
                 Brands = GetBrands(),
                 Categories = GetCategories(),
                 Products = GetProducts()
             };


         return feed;
    }

Here is my model Feed. Note the following doesn't seem to turn the element into an attribute

[XmlAttribute(AttributeName = "extractDate")]
public class Feed
{
    [XmlAttribute(AttributeName = "extractDate")] //attribute is ignored
    public string extractDate { get; set; }
    public bool incremental { get; set; }
    public string name { get; set; }
    public List<Brand> Brands { get; set; }
    public List<Category> Categories { get; set; } 
    public List<Product> Products { get; set; } 
}

How do i output

<feed extractDate="2012/01/01" 

// other logic

/>
like image 262
frosty Avatar asked Jun 03 '13 11:06

frosty


People also ask

What is model binding in web API?

The model binding system: Retrieves data from various sources such as route data, form fields, and query strings. Provides the data to controllers and Razor pages in method parameters and public properties. Converts string data to . NET types.

How do we do parameter binding in web API?

When Web API calls a method on a controller, it must set values for the parameters, a process called binding. By default, Web API uses the following rules to bind parameters: If the parameter is a "simple" type, Web API tries to get the value from the URI.

Can we pass XML in web API?

The REST API Client Service currently accepts only JSON input when making REST API Create, Read, Update, or Delete requests. It is nevertheless possible to use XML input when making REST API requests.


2 Answers

Web API by default uses DataContractSerializer in XmlMediaTypeFormatter and probably that's the reason you are not seeing your attribute decorations taking effect. Do you have the XmlSerializer enabled on the XmlMediaTypeFormatter to see your expected output?

config.Formatters.XmlFormatter.UseXmlSerializer = true;

Also, you could set XmlSerializer only for specific types too using the following api:

config.Formatters.XmlFormatter.SetSerializer<>

like image 200
Kiran Avatar answered Oct 09 '22 17:10

Kiran


Edit
Managed to simulate your issue with a blank project and Kiran's answer seems to do the trick.
Just add this line in your controller(for testing purposes, it should probably be in your global.asax)

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

Do you have the [XmlRoot] on top of your class or is it missing?
Not sure the attribute will work without an xml class decorator.
A simple sanity check you could do is serialize the class without web api involved to make sure it's nothing silly but actually web api related.

like image 39
Kristof Avatar answered Oct 09 '22 15:10

Kristof