可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have been struggling with outputting a custom root xml element when returning a list of objects in my WebAPI controller.
My controller method looks something like this:
public List<Product> Get()
{
return repository.GetProducts();
}
which renders an xml output like this:
<ArrayOfProduct>
<Product>
<Name>Product1</Name>
</Product>
<Product>
<Name>Product2</Name>
</Product>
</ArrayOfProduct>
I would like to change <ArrayOfProduct>
to <Products>
but haven't found a way of doing so.
I have tried different variations of the DataContract
and DataMember
attributes to no avail.
Does anyone know if there is a way of doing what I want, short of wrapping my List<Product>
object in a new class and returning that instead?
回答1:
I know you are not fond of the wrapper idea but there is a solution that somewhat uses a wrapper but also uses the xml attributes which are very easy to work with. My refusal to using the following approach is the use of the old serializer.
public class Product
{
[XmlAttribute( "id" )]
public int Id
{
get;
set;
}
[XmlAttribute( "name" )]
public string Name
{
get;
set;
}
[XmlAttribute( "quantity" )]
public int Quantity
{
get;
set;
}
}
[XmlRoot( "Products" )]
public class Products
{
[XmlAttribute( "nid" )]
public int Id
{
get;
set;
}
[XmlElement(ElementName = "Product")]
public List<Product> AllProducts { get; set; }
}
Now your controller can just return Products like:
public Products Get()
{
return new Products
{
AllProducts = new List<Product>
{
new Product {Id = 1, Name = "Product1", Quantity = 20},
new Product {Id = 2, Name = "Product2", Quantity = 37},
new Product {Id = 3, Name = "Product3", Quantity = 6},
new Product {Id = 4, Name = "Product4", Quantity = 2},
new Product {Id = 5, Name = "Product5", Quantity = 50},
}
};
}
now you can specify the serializer in start-up like so:
var productssXmlFormatter = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
productssXmlFormatter.SetSerializer<Products>( new XmlSerializer( typeof( Products ) ) );
I know it is not the most ideal way as to having to specify the serializer and losing the flexability and convenience of EF and Linq. Or at least having to intervene rather than just returning IEnumerable<>.
I have to give credit to the following site as I first learned of this way from the site at: http://justthisguy.co.uk/outputting-custom-xml-net-web-api/
This will result in the following xml:
<Products nid="0">
<Product id="1" name="Product1" quantity="20"/>
<Product id="2" name="Product2" quantity="37"/>
<Product id="3" name="Product3" quantity="6"/>
<Product id="4" name="Product4" quantity="2"/>
<Product id="5" name="Product5" quantity="50"/>
</Products>
Please don't forget to look at the site listed.
回答2:
class Program
{
static void Main(string[] args)
{
HttpConfiguration config = new HttpConfiguration();
DataContractSerializer productsSerializer = new DataContractSerializer(typeof(List<Product>), "Products", String.Empty);
config.Formatters.XmlFormatter.SetSerializer(typeof(List<Product>), productsSerializer);
config.Formatters.XmlFormatter.Indent = true;
config.Formatters.XmlFormatter.WriteToStreamAsync(
typeof(List<Product>),
new List<Product> { new Product { Name = "Product1" }, new Product { Name = "Product2" } },
Console.OpenStandardOutput(),
null,
null).Wait();
Console.WriteLine();
}
}
[DataContract(Namespace = "")]
public class Product
{
[DataMember]
public string Name { get; set; }
}
回答3:
As elusive said....
[XmlArray("Products")]
public List<Product> Get()
{
return repository.GetProducts();
}
If you are having issues because it is a method, then try redefining it as a property since it takes no parameters, and probably makes more sense.
[XmlArray("Products")]
public List<Product> Products {
get {
return repository.GetProducts();
}
}
That will list all Product
items inside an element element named 'Products'.
<Products>
<Product>
<Name>Product1</Name>
</Product>
<Product>
<Name>Product2</Name>
</Product>
</Products>
P.S. To rename the Product
item tags you can use the [XmlArrayItem("Product")]
attribute to do this. If you want to have a flat list (don't wrap them in 'Products'), you can use the [XmlElement("Product")]
and that will list them without grouping them.
回答4:
I guess you're using NetDataContractSerializer
You can specify the root element name in the serializer's constructor :
NetDataContractSerializer serializer = new NetDataContractSerializer("Products", "Your.Namespace");
Take a look at the differents overloads :
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.netdatacontractserializer.aspx
回答5:
I was facing the same issue and searched but could not found any proper solution so finally decided to replace the string "ArrayOfProduct" with "Products" and it is working smooth.
I know it is bad way to do this but it is quick.
回答6:
You should be able to use Xml serialization attirbues as outlined here
Its been a while since I've worked with them, but they were quick and easy to learn.
回答7:
Please check this out. There is some information about how to configure the output of the class in xml format.
https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute%28v=vs.110%29.aspx
In my opinion, it is not about the wep api but the configuration of the class.
回答8:
I have done this many times, customizing xml serialization output. Using attributes in my class definitions:
[XmlArray("Products")]
public List<Product> Products
{
get { return repository.GetProducts(); }
}
this should get you the output you are seeking...