1、命名空间的问题
* 1.1 XML示例:

* 1.2 反序列化代码:
点击查看源代码 ```
public static object DeserializeFromXml<T>(string xmlFilePath)
{
object result = null;
using (FileStream fs = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
XmlSerializer xs = new XmlSerializer(typeof(T));
result = xs.Deserialize(fs);
fs.Close();
fs.Dispose();
}
return result;
}
```
</details> </code></pre></li>
* 1.3 程序报异常:

* 1.4 修复异常:
将1.2中代码里new XmlSerializer处增加命名空间;如下:
XmlSerializer xs = new XmlSerializer(type, "[http://www.w3.org/2000/09/xmldsig#](http://www.w3.org/2000/09/xmldsig#)");
2、反序列化成实体时,List为空的异常
* 2.1 XML实例:

* 2.2 实体示例:

* 2.3 程序报异常:
最终实体里的两个List均为空(count=0);
* 2.4 修复异常:
XmlArrayAttribute + XmlArrayItemAttribute 在序列化具有外部容器元素的集合时应使用。当没有外部容器时,应使用 XmlElementAttribute;
将实体List前面增加注解XmlElementAttribute,如下图:
![]()
手机扫一扫
移动阅读更方便
你可能感兴趣的文章