6

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

/>
2

3 Answers 3

11

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<>

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

Comments

3

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.

2 Comments

thanks, very useful. So i've added [XmlRoot] above my class name with no change I've just tested using the xml.serializer and the attributes are correct in this version. Is there a way i could output the serialize version from web api.
This line of code fixed the problem I was having with Web Api "ignoring" the XmlRootAttribute and XmlTypeAttribute of my complex types being used as parameters and returns.
0

How about this:

[XmlRoot("feed")]
public class Feed
{
     [XmlAttribute(AttributeName = "extractDate")]
     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; } 
}

3 Comments

hi, thanks for your comment. I've tried this. But still doesn't effect the output. Does this work for you?
It does actually. It might be stupid, but try putting an [XmlElement] on top of the others fields...
and don't forget about GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

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.