如果我使用XmlSerializer序列化以下代码。
[XmlRoot("products")]
public class Products : List<Product>
{
}
public class Product
{
}
我得到以下xml
<ArrayOfProduct>
<Product/>
</ArrayOfProduct>
如何编写以获得以下标签命名(产品和小写产品)?
<products>
<product/>
</products>
答案 0 :(得分:2)
简单;不要继承List<T>
:
[XmlRoot("products")]
public class ProductWrapper
{
private List<Product> products = new List<Product>();
[XmlElement("product")]
public List<Product> Products { get {return products; } }
}
public class Product
{
}
答案 1 :(得分:1)
你是如何进行序列化的?我使用了以下代码:
Products products = new Products();
products.Add(new Product());
XmlSerializer serializer = new XmlSerializer(typeof(Products));
using (StringWriter sw = new StringWriter())
{
serializer.Serialize(sw, products);
string serializedString = sw.ToString();
}
得到了这个结果:
<?xml version="1.0" encoding="utf-16"?>
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Product />
</products>