加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – 为什么XmlSerializer没有序列化我的数组?

发布时间:2020-12-16 23:05:34 所属栏目:百科 来源:网络整理
导读:我是XmlSerializer的新手.我写了一个小类来保存数据库中的条目: [Serializable]public struct Entry{ public string artkey,lid,request,status,requestdate;} 很简单吧?序列化这些列表应该是小菜一碟. 我有一个编译这些列表的函数.要序列化我的列表,我尝
我是XmlSerializer的新手.我写了一个小类来保存数据库中的条目:

[Serializable]
public struct Entry
{
    public string artkey,lid,request,status,requestdate;
}

很简单吧?序列化这些列表应该是小菜一碟.

我有一个编译这些列表的函数.要序列化我的列表,我尝试以下代码:

XmlSerializer serializer = new XmlSerializer(typeof(Entry));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
serializer.Serialize(ms,entries.ToArray());
ms.WriteTo(Response.OutputStream);

此代码打印以下异常:

<error>System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidCastException: Specified cast is not valid.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterEntry.Write3_Entry(Object o)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,Object o,XmlSerializerNamespaces namespaces,String encodingStyle,String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream,XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream,Object o)
   at CCB_Requests.xmlResponse_selectFromCcb_Requests(HttpResponse response)
   at CCB_Requests.ProcessRequest(HttpContext context)</error>

看来我一定是犯了一个简单的错误.我怎样才能解决这个问题?

解决方法

您正在序列化一个Entry数组,将XmlSerializer的初始化更改为:

// typeof(Entry) ==> typeof(Entry[])
XmlSerializer serializer = new XmlSerializer(typeof(Entry[]));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读