无法将类型为“System.Xml.XmlElement”的对象强制转换为类型“System.Xml.XmlDocument”。

MaNong_plf 2015-06-02 11:22:27
请熟悉XML的大侠帮帮忙,小弟对XML不是很了解,谢谢。现在分不多,以后补上
一.webservice中的方法
public System.Xml.XmlDocument GetUpdateDate() {
//取得更新的xml模板内容
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Update.xml"));
XmlElement root = doc.DocumentElement;
//看看有几个文件需要更新
XmlNode updateNode = root.SelectSingleNode("filelist");
string path = updateNode.Attributes["sourcepath"].Value;
int count = int.Parse(updateNode.Attributes["count"].Value);
//将xml中的value用实际内容替换
for (int i = 0; i < count; i++) {
XmlNode itemNode = updateNode.ChildNodes[i];
string fileName = path + itemNode.Attributes["name"].Value;
FileStream fs = File.OpenRead(Server.MapPath(fileName));
itemNode.Attributes["size"].Value = fs.Length.ToString();
BinaryReader br = new BinaryReader(fs);
//这里是文件的实际内容,使用了Base64String编码
itemNode.SelectSingleNode("value").InnerText = Convert.ToBase64String(br.ReadBytes((int)fs.Length), 0, (int)fs.Length);
br.Close();
fs.Close();
}
return doc;
}

二.winform后台代码
这个代码是从网上看到的。怎么自己用起来就有问题呢?希望各位大神帮帮忙!我主要是为了实现 winform自动升级功能。
localhost.WebServiceAuto Start= new localhost.WebServiceAuto(); //winform程序引用
调用System.Xml.XmlDocument doc = (System.Xml.XmlDocument)Start.GetUpdateDate();在这报错无法将类型为“System.Xml.XmlElement”的对象强制转换为类型“System.Xml.XmlDocument”。
...全文
416 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
江南小鱼 2015-06-02
  • 打赏
  • 举报
回复
Start.GetUpdateDate改成返回xml字符串吧,然后通过xml字符串生成XmlDocument。 webservice估计不能直接返回XmlDocument
江南小鱼 2015-06-02
  • 打赏
  • 举报
回复
引用 3 楼 u014083929 的回复:
[quote=引用 1 楼 lovelj2012 的回复:] Start.GetUpdateDate改成返回xml字符串吧,然后通过xml字符串生成XmlDocument。 webservice估计不能直接返回XmlDocument
转换成xml字符组你是指 返回值类型改为string类型的吗?这样使用不行。这段代码System.Xml.XmlDocument doc = (System.Xml.XmlDocument)Start.GetUpdateDate();会提示:无法将string类型转化为System.Xml.XmlDocument 。。。。哎,谁还有好的方法教教我! [/quote] 不告诉你了么? webservice不能直接返回XmlDocument。 方法改成返回xml字符串,客户端转换成XmlDocument
exception92 2015-06-02
  • 打赏
  • 举报
回复
webservice 返回的是string类型的xml格式,没有办法直接返回成XmlDocument 对象。 你将 返回的Stringxml 转换成xml格式,可以使用linq to xml 的XElement.Parse 方法。 http://blog.csdn.net/duanzi_peng/article/details/24018431 另:一般的WebService 返回数据为数组集合。
MaNong_plf 2015-06-02
  • 打赏
  • 举报
回复
引用 1 楼 lovelj2012 的回复:
Start.GetUpdateDate改成返回xml字符串吧,然后通过xml字符串生成XmlDocument。 webservice估计不能直接返回XmlDocument
转换成xml字符组你是指 返回值类型改为string类型的吗?这样使用不行。这段代码System.Xml.XmlDocument doc = (System.Xml.XmlDocument)Start.GetUpdateDate();会提示:无法将string类型转化为System.Xml.XmlDocument 。。。。哎,谁还有好的方法教教我!
MaNong_plf 2015-06-02
  • 打赏
  • 举报
回复
转换成xml字符组你是指 返回值类型改为string类型的吗?这样使用不行。这段代码System.Xml.XmlDocument doc = (System.Xml.XmlDocument)Start.GetUpdateDate();会提示:无法将string类型转化为System.Xml.XmlDocument 。。。。哎,谁还有好的方法教教我!

110,534

社区成员

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

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

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