Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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_Xml Parsing - Fatal编程技术网

C# 子节点未正确序列化

C# 子节点未正确序列化,c#,xml,xml-parsing,C#,Xml,Xml Parsing,试图使模型的xml序列化正常工作时遇到问题 这是我的模型: [XmlRoot(ElementName = "Invoice", Namespace = "", IsNullable = false)] public class Invoice { [XmlElement(ElementName = "Items")] public virtual List<Item> Items{ get; set; } } [XmlRoot

试图使模型的xml序列化正常工作时遇到问题

这是我的模型:

 [XmlRoot(ElementName = "Invoice", Namespace = "", IsNullable = false)]
    public class Invoice
    {
        [XmlElement(ElementName = "Items")]
        public virtual List<Item> Items{ get; set; }
    }
[XmlRoot(ElementName = "Item")]
    public class Item
    {
        [XmlAttribute(AttributeName = "Line")]
        public virtual int Line { get; set; }

        [XmlAttribute(AttributeName = "MatNum")]
        public virtual string MatNum { get; set; }
    }
[XmlRoot(ElementName=“Invoice”,Namespace=”“,IsNullable=false)]
公共类发票
{
[xmlement(ElementName=“Items”)]
公共虚拟列表项{get;set;}
}
[XmlRoot(ElementName=“Item”)]
公共类项目
{
[XmlAttribute(AttributeName=“Line”)]
公共虚拟整数行{get;set;}
[XmlAttribute(AttributeName=“MatNum”)]
公共虚拟字符串MatNum{get;set;}
}
这将导致以下错误的XML:

<?xml version="1.0" encoding="utf-16"?>
<Invoice>
  <Items Line="1" MatNum="Beer">
  <Items Line="2" MatNum="Cola">
</Invoice>

结果应该如下所示:

<?xml version="1.0" encoding="utf-16"?>
<Invoice>
  <Items>
    <Item Line="1" MatNum="Beer">
    <Item Line="2" MatNum="Cola">
  </Items>
</Invoice>


我做错了什么?XML序列化程序似乎忽略了子类元素。

好吧,这是一个愚蠢的错误,需要更改为XmlArrayItem而不是“Items”的元素

[XmlRoot(ElementName=“Invoice”,Namespace=”“,IsNullable=false)]
公共类发票
{
[XmlArrayItem(ElementName=“Item”)]
公共虚拟列表项{get;set;}
}
[XmlRoot(ElementName=“Item”)]
公共类项目
{
[XmlAttribute(AttributeName=“Line”)]
公共虚拟整数行{get;set;}
[XmlAttribute(AttributeName=“MatNum”)]
公共虚拟字符串MatNum{get;set;}
}

查找XmlArrayAttribute和XmlArrayItemAttribute
[XmlRoot(ElementName = "Invoice", Namespace = "", IsNullable = false)]
    public class Invoice
    {
        [XmlArrayItem(ElementName = "Item")]
        public virtual List<Item> Items { get; set; }
    }
[XmlRoot(ElementName = "Item")]
    public class Item
    {
        [XmlAttribute(AttributeName = "Line")]
        public virtual int Line { get; set; }

        [XmlAttribute(AttributeName = "MatNum")]
        public virtual string MatNum { get; set; }
    }