HashTable写入Xml的问题

ouyang4683 2009-12-07 02:37:50

 //Station是个类
 //StationS是HashTable,存的Station
        XmlSerializer serializer = null;
FileStream stream = null;
try
{
serializer = new XmlSerializer(typeof(Station[]));
stream = new FileStream(Application.StartupPath + "\\le.xml", FileMode.Open, FileAccess.Write);
serializer.Serialize(stream, (Station[])StationS.Values);
}

错误提示:无法将类型为“ValueCollection”的对象强制转换为类型“Station[]”。

要怎么改呢
...全文
225 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
INTTNY 2009-12-07
  • 打赏
  • 举报
回复
缺少了点东西呢

serializer = new XmlSerializer(typeof(Station[]));
stream = new FileStream(Application.StartupPath + "\\Visible.xml", FileMode.Open, FileAccess.Write);

List <Station> stationList = new List <Station>(StationS.Values.Count);

foreach (Station s in StationS.Values)
{
stationList.Add(s);
}

//加这个
Station[] stationArray = stationList.ToArray();

serializer.Serialize(stream, stationArray); //这里也改下
ouyang4683 2009-12-07
  • 打赏
  • 举报
回复
serializer = new XmlSerializer(typeof(Station[]));
stream = new FileStream(Application.StartupPath + "\\Visible.xml", FileMode.Open, FileAccess.Write);

List<Station> stationList = new List<Station>(StationS.Values.Count);

foreach (Station s in StationS.Values)
{
stationList.Add(s);
}

serializer.Serialize(stream, stationList);
INTTNY 2009-12-07
  • 打赏
  • 举报
回复
你这个不对啊,你的stationList里边的是个什么东西?把你改动后的代码贴上来看下
ouyang4683 2009-12-07
  • 打赏
  • 举报
回复
那我想把Hashtable 写入XML要怎么写呢。。。
真相重于对错 2009-12-07
  • 打赏
  • 举报
回复

Hashtable.values 只是一个 ICollection 接口,不是数组

ouyang4683 2009-12-07
  • 打赏
  • 举报
回复
问题又来了。。。
serializer.Serialize(stream, stationValues);

无法将类型为“System.Collections.Generic.List`1[StationS]”的对象强制转换为类型“Station
INTTNY 2009-12-07
  • 打赏
  • 举报
回复
你用的是.NET2.0框架吧,用的VS2005?

那样的话你就只能够用foreach来逐个添加了

foreach (Station s in StationS.Values.GetEnumerator())
{
stationList.Add(s);
}
LutzMark 2009-12-07
  • 打赏
  • 举报
回复

using (FileStream fs = new FileStream(savekeyfile, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<string>));
List<string> li = new List<string>();
//这里假设哈希表里存的值都是字符串
foreach (string s in ht.Values)
{
li.Add(s);
}
serializer.Serialize(fs, li);
fs.Close();
}

ouyang4683 2009-12-07
  • 打赏
  • 举报
回复
啊,呵呵 不是的,是我没说清

StationS是HashTable 

所以StationS.Values. 后面 没有OfType


试过
stationList.AddRange((Station[])StationS.Values);
也不好用
LutzMark 2009-12-07
  • 打赏
  • 举报
回复
Hashtable没实现 IDictionary,不能被序列化为xml
INTTNY 2009-12-07
  • 打赏
  • 举报
回复
嗯……

using System.Collections.Generic;
ouyang4683 2009-12-07
  • 打赏
  • 举报
回复
OfType ?有这个东西么
INTTNY 2009-12-07
  • 打赏
  • 举报
回复
错了,是这样改

stationList.AddRange(StationS.Values.OfType <Station>());
INTTNY 2009-12-07
  • 打赏
  • 举报
回复
把那句话改下吧

stationList.AddRange(StationS.Values.OfType<string>());
真相重于对错 2009-12-07
  • 打赏
  • 举报
回复
Hashtable 和数组 是不一样的类型
INTTNY 2009-12-07
  • 打赏
  • 举报
回复
报的什么错误?
ouyang4683 2009-12-07
  • 打赏
  • 举报
回复
一样的, stationList.AddRange(StationS.Values);  这写不进来
INTTNY 2009-12-07
  • 打赏
  • 举报
回复
try
{
serializer = new XmlSerializer(typeof(Station[]));
stream = new FileStream(Application.StartupPath + "\\le.xml", FileMode.Open, FileAccess.Write);
//这里改改吧
List<Station> stationList = new List<Station>(StattionS.Values.Count);
stationList.AddRange(StationS.Values);
Station[] stationValues = stationList.ToArray();
serializer.Serialize(stream, stationValues);
}

111,112

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧