如果一个xml文件是这样的格式:
<WMWROOT xmlns="http://www.123455/soap">
<WMWDATA>
<WMFWUpload xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<rows><row>....</row><row>....</row></rows>
......
</WMFWUpload>
</WMWDATA>
</WMWROOT>
在我们使用发序列化的时候,必须显示的在类上加上命名空间:
[XmlRoot(Namespace = "http://www.123455/soap", ElementName = "WMWROOT")]
public class WMWROOT_123
{
...
// 反序列化代码:
public static List<row> Format(string file, out string msg)
{
WMWROOT_123 _result = new WMWROOT_123();
msg = "";
XmlSerializer mySerializer = new XmlSerializer(typeof(WMWROOT_123));
try
{
using (FileStream fs = new FileStream(file, FileMode.Open))
{
_result = (WMWROOT_123)mySerializer.Deserialize(fs);
}
return _result.wmwdata.wmfwload.rows.ToList();
}
catch (Exception ex)
{
msg = ex.Message + " -- Details: " + ex.InnerException;
}
return new List<row>();
}
}