我在WEBSERVICE中定义一个类        [WebMethod(Description = "在线更新软件")]
        public System.Xml.XmlDocument GetUpdateData()
        {
            //取得更新的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;
        }但是我在客户端引用的时候出现问题:
无法将类型为“System.Xml.XmlElement”的对象强制转换为类型“System.Xml.XmlDocument”。
        public void update()
        {
            WebSvs.Service1 webs = new WebSvs.Service1();
            System.Xml.XmlDocument doc = ((System.Xml.XmlDocument)webs.GetUpdateData());
          
            doc.Save(Application.StartupPath + @"\update.xml");
            System.Diagnostics.Process.Start(Application.StartupPath + @"\update.exe");
            Close();
            Application.Exit();
        }

解决方案 »

  1.   

        [WebMethod(Description = "在线更新软件")]
            public string GetUpdateData()
            {
                //取得更新的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.InnerXml;
            }      public void update()
            {
                WebSvs.Service1 webs = new WebSvs.Service1();
                System.Xml.XmlDocument doc = new XmlDocument();[color]
     string xml= webs.GetUpdateData();
    doc.LoadXml(xml);
              
                doc.Save(Application.StartupPath + @"\update.xml");
                System.Diagnostics.Process.Start(Application.StartupPath + @"\update.exe");
                Close();
                Application.Exit();
            }
    webservice最好不好返回引用的类型
      

  2.   

    如果是引用的话
    不需要转换吧
    System.Xml.XmlDocument doc = webs.GetUpdateData();
      

  3.   

    webservice还有这个功能呀,我的客户端升级程序是直接到指定的服务器上下载的,呵呵!
      

  4.   

    如果是按照4楼的方法,出现下面的错误
    错误 1 无法将类型“System.Xml.XmlNode”隐式转换为“System.Xml.XmlDocument”。存在一个显式转换(是否缺少强制转换?) E:\temp\update\WindowsApplication1\WindowsApplication1\Form1.cs 40 42 WindowsApplication1
      

  5.   

    你的返回值就是XmlNode型的吧.那前面就不用转了.
      

  6.   

    webservice客户端使用的时候整体返回的是一个document,所有函数的返回值都是一个node,你要作为一个document使用,只能自己转换一下,使用parseXml方法吧