C# 如何重命名类中的序列化根对象名?
(首先,请随意编辑我的标题,我真的找不到更好的标题来解决我的问题) 我得到了我的根类:C# 如何重命名类中的序列化根对象名?,c#,xml,serialization,C#,Xml,Serialization,(首先,请随意编辑我的标题,我真的找不到更好的标题来解决我的问题) 我得到了我的根类: [XmlRoot("ProductList")] public class Product { [XmlElement("Property1")] public string property1 { get; set; } [XmlElement("Property2")] public Property2 property2 { get; set; } [XmlArr
[XmlRoot("ProductList")]
public class Product
{
[XmlElement("Property1")]
public string property1 { get; set; }
[XmlElement("Property2")]
public Property2 property2 { get; set; }
[XmlArray("Property3Array")]
[XmlArrayItem("Property3ArrayItem")]
public List<Property3> property3{ get; set; }
}
序列化工作正常,但在我的products.xml
文件中,根节点是:
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
</ArrayOfProduct>
...
但我希望列表的根元素命名为ProductList
我尝试了[XmlRoot(“ProductList”)]
,但没有成功
那么,如何重命名列表的根xml对象的名称呢?使用接受XmlRootAttribute的
var xs = new XmlSerializer(typeof(List<Product>), new XmlRootAttribute("ProductList"));
var xs=new-XmlSerializer(typeof(List),new-XmlRootAttribute(“ProductList”);
请参阅以获得一个工作演示。“但是我希望列表的根元素命名为ProductList
”-然后不要序列化Product[]
,而是创建一个holder类型(命名为…ProductList
)。@Sinatr我真的应该休息一下,因为我甚至没有想到这一点。。。是的,你说得对。但出于问题的考虑,这是否可以重命名为具有某些属性的根元素?例如[XmlRoot(“产品列表”)]
。还有一点,在序列化之后,根对象被命名为ArrayOf…
。如何摆脱这个问题?您应该已经能够使用xmlementattribute
。遗憾的是,xmlementattribute
无法装饰classHmm?我看到链接的问题了吗?将数组放入ProductList
类属性中,并向其添加属性。
var xs = new XmlSerializer(typeof(List<Product>), new XmlRootAttribute("ProductList"));