Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何重命名类中的序列化根对象名?_C#_Xml_Serialization - Fatal编程技术网

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"));