Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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中对象的ArrayList_C#_.net_Xml_Deserialization - Fatal编程技术网

C# 将xml反序列化为C中对象的ArrayList

C# 将xml反序列化为C中对象的ArrayList,c#,.net,xml,deserialization,C#,.net,Xml,Deserialization,以下xml是序列化资产对象的ArrayList的结果 <ArrayOfAsset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Asset> <name>bill</name> <type>perosn</type> </Asset>

以下xml是序列化资产对象的ArrayList的结果

<ArrayOfAsset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Asset>
    <name>bill</name>
    <type>perosn</type>
  </Asset>
  <Asset>
    <name>bill</name>
    <type>perosn</type>
  </Asset>
 </ArrayOfAsset> 
我可以用默认的C反序列化程序反序列化它,没有问题。如果我的根元素从ArrayOfAsset更改为assets,我的反序列化程序就会崩溃。如何使我的反序列化程序知道此更改

以下是我的反序列化代码:

  StreamReader sr = new StreamReader("c:\\assest.xml");
  string r = sr.ReadToEnd();
  List<Asset> list;
  Type[] extraTypes = new Type[1];
  extraTypes[0] = typeof(Asset);
  System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<Asset>), extraTypes);
  object obj = serializer.Deserialize(xReader);
   list = (List<Asset>)obj;

我也有同样的问题

MSDN文件中规定:

注意,XmlSerializer无法反序列化以下内容:ArrayList数组和List数组

但我真的不知道这是否意味着不能反序列化ArrayList或ArrayList的数组。。。我不清楚


使用服务堆栈序列化程序:

一旦使用默认序列化,就不能更改XML格式。为了支持经过调整的XML,您必须提供一些覆盖默认序列化的元数据。例如:


此外,将属性附加到您的资产类别和相关资产类别也是值得的。通过这种方式,您可以描述[XmlArray]和[XmlArrayItem]以支持所需的标记/子标记

序列化程序是编写您正在反序列化的XML,还是有其他源?XmlSerializer类希望XML与它生成的格式相同,而不是您的任意格式。您需要为此编写自己的XML反序列化类。