Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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# 自定义列表与嵌套列表的Xml序列化_C#_Xml_Serialization - Fatal编程技术网

C# 自定义列表与嵌套列表的Xml序列化

C# 自定义列表与嵌套列表的Xml序列化,c#,xml,serialization,C#,Xml,Serialization,我在这里(和其他地方)寻找过这个问题的答案,但没有找到任何答案 我有一个从List派生的类,它包含另一个List。当我序列化为XML时,不包括内部集合的内容 下面是一个非常简单的示例: [Serializable()] public class MyList<T> : List<T> { protected List<T> _nestedList= new List<T>(); public List<T> Neste

我在这里(和其他地方)寻找过这个问题的答案,但没有找到任何答案

我有一个从List派生的类,它包含另一个List。当我序列化为XML时,不包括内部集合的内容

下面是一个非常简单的示例:

[Serializable()]
public class MyList<T> : List<T>
{
    protected List<T> _nestedList= new List<T>();

    public List<T> NestedList
    {
        get { return _nestedList; }
        set { _nestedList = value; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyList<int> ints = new MyList<int>();
        ints.Add(1);
        ints.Add(2);
        ints.NestedList.Add(3);
        ints.NestedList.Add(4);

        string xml;
        var xmlSerializer = new XmlSerializer(ints.GetType());
        using (var memoryStream3 = new MemoryStream())
        {
            xmlSerializer.Serialize(memoryStream3, ints);
            xml = Encoding.UTF8.GetString(memoryStream3.ToArray());
        }
    }
}
[Serializable()]
公共类MyList:列表
{
受保护列表_nestedList=新列表();
公共列表嵌套列表
{
获取{return\u nestedList;}
设置{u nestedList=value;}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
MyList ints=新的MyList();
加入(1);
内加(2);
内部嵌套列表添加(3);
内部嵌套列表添加(4);
字符串xml;
var xmlSerializer=新的xmlSerializer(ints.GetType());
使用(var MemoryStream 3=new MemoryStream())
{
Serialize(memoryStream3,ints);
xml=Encoding.UTF8.GetString(memoryStream3.ToArray());
}
}
}
以下是xml字符串:

<?xml version="1.0"?>
<ArrayOfInt xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <int>1</int>
  <int>2</int>
</ArrayOfInt>

1.
2.
注意NestedList的缺乏

我在NestedList上尝试了各种属性,如XmlArray或XmlArrayItem,但这些都不起作用

我也知道这不会正确地反序列化。实际模型比这更复杂;此集合位于另一个对象的内部,该对象也标记为可序列化

行为是相同的:外部列表序列化,但内部列表不序列化。我试图缩小问题的范围,所以我将展示这个较小的示例

提前谢谢


编辑:看起来这是设计的,XmlSerializer在序列化期间不包含集合的属性。因此,我必须进行自定义序列化。

只需将其标记为已回答,这是在XmlSerializer中设计的。

的可能副本