XML Formatting

Topics: ASP.NET Web API
Apr 24, 2012 at 4:16 PM
Edited Apr 24, 2012 at 4:18 PM

I'm having difficulty on understanding how to get my IQueryable formatted properly when serializing to XML. I need the XML body to contain a specific root element and namespace. I also need to support JSON and some other formats. Is this possible to accomplish this with the current XmlMediaFormatter?

public IQueryable<Companies> Get()
{
    return new[]{new Company{Name = "CompanyA"},new Company{Name = "CompanyB"}}.AsQueryable();
}

XML output:

<companies xmlns="http://mycompany">
  <company name="CompanyA"/>
  <company name="CompanyB"/>
</company>
public class Company
{
    [XmlElement("name")] 
    public string Name { get; set; } 
}
[XmlRoot("companies", Namespace = "http://mycompany")]
public class CompanyGroup 
{
    [XmlElement("company")] 
    public Collection<Company> Companies { get; set; } 
}