本帖最后由 Jelindu 于 2009-08-06 13:35:19 编辑

解决方案 »

  1.   


    xml处理类   xmldocument   xmlreader   xmlwriter   datasetxml文档转为字符串后再发,XMLDOCMENT.OUTXML  
      

  2.   

    两个难点
    1:怎样转换xml文档的格式为为字符串
    2:怎样发给指定的地址
    给点实际demo!
      

  3.   

    如果能给我提供他们方法,就好了,就可以像下面那样调用,但他们要我用http协议传,我没搞过,网上也查不到 
    localhost1.Service1 s = new CommunicatePort.Web.localhost1.Service1();
                int a = s.GetSum(1,2);
      

  4.   

    post 传 就可以给你的什么地址
      

  5.   

    XmlDataDocument.OuterXml就是 xml 的字符串形式
      

  6.   


            XmlDocument doc = new XmlDocument();
            doc.Load("fileName");
            string form = string.Format("<form id='DynamicForm' action='xxx.aspx'><input type='hidden' value='{0}' /></form><script>document.getElementById('DynamicForm').submit();</script>", doc.OuterXml);
            Response.Write(form);
      

  7.   

    你不是穿字符串形式的 xml 么, 不用序列化
      

  8.   

    利用XMLHttpRequest对象异步发送XML文档
    http://blog.csdn.net/Sandy945/archive/2009/05/12/4170512.aspx
      

  9.   

    xml格式不用转换为字符串,本来xml格式就是个“字符串”,soap消息就是xml格式的,只是对xml数据需要进行序列化,使用XmlSerializer来对其进行序列化。<?xml version="1.0" encoding="UTF-8"?><hh xmlns:xsi="http://www.w3.org/2001/XMLSchema-nstance"><a><b>b</b><c>c</c><d>1.0</d><e><f><g>g</g><h>h</h></f></e></a></hh>如果对方给你的并非webservice,那么你尝试使用HttpWebRequest来发送请求,请求地址为对方给的地址,方式为POST,内容为上面的xml(不需要手动序列化),然后用HttpWebResponse来接收返回信息。如果对方公布的接口是webservice,那么就更好办了,你根据其公布的url生成一个代理类,用代理类来呼叫器接口就好了。
      

  10.   

    参考 HttpWebRequest   和   HttpWebResponse   类的内容
      

  11.   

    可以通过HttpWebRequest 和 responsestream 来实现//ws_url为提供的WebService地址
     public static void FindMainID3(string ws_url, string ClientJob, string mainID, ref DataTable dsmain, ref DataTable dsline, ref DataTable dssubline, string linetag, string sublinetag)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ws_url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "Post";
            XmlDocument requestdom = new XmlDocument();
            XmlElement reqrootele = requestdom.CreateElement("Steel-ERP");
            requestdom.AppendChild(reqrootele);        XmlElement reqheadele = requestdom.CreateElement("Head");
            reqheadele.SetAttribute("SessionID", "681A4E9A140F42E4E044001321BD1C8B");
            reqrootele.AppendChild(reqheadele);        XmlElement reqbodyele = requestdom.CreateElement("Body");
            reqrootele.AppendChild(reqbodyele);        XmlElement reqclientele = requestdom.CreateElement("Client");
            reqclientele.SetAttribute("Job", ClientJob);
            reqbodyele.AppendChild(reqclientele);        XmlElement reqmainele = requestdom.CreateElement("Main");
            reqmainele.SetAttribute("Job", "Find");
            reqmainele.SetAttribute("ID", mainID);
            reqclientele.AppendChild(reqmainele);        string data = requestdom.InnerXml;
            byte[] byteData = Encoding.UTF8.GetBytes(data);        request.ContentLength = byteData.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(byteData, 0, byteData.Length);
            }        Stream responsestream;
            try
            {
                responsestream = request.GetResponse().GetResponseStream();
            }
            catch (Exception e)
            {
                throw new Exception("客户端POST数据发生异常:" + e.Message);
            }
            XmlDocument responsedom = new XmlDocument();
            responsedom.Load(responsestream);
            XmlElement mainele = (XmlElement)responsedom.GetElementsByTagName("Main")[0];
            DataRow mainrow = dsmain.NewRow();
            for (int i = 0; i < mainele.Attributes.Count; i++)
            {
                if (!dsmain.Columns.Contains(mainele.Attributes[i].Name))
                {
                    dsmain.Columns.Add(mainele.Attributes[i].Name);
                }
                
                mainrow[i] = StringUtil.IsExistAttr(mainele.Attributes[i]);
            }
            dsmain.Rows.Add(mainrow);
            DataRow linerow;
            foreach (XmlElement ele in mainele.GetElementsByTagName("Line1"))
            {
                DataRow subrow;
                foreach (XmlElement subele in ele.GetElementsByTagName("SubLine1"))
                {
                    subrow = dssubline.NewRow();
                    for (int i = 0; i < subele.Attributes.Count; i++)
                    {
                        if (!dssubline.Columns.Contains(subele.Attributes[i].Name))
                        {
                            dssubline.Columns.Add(subele.Attributes[i].Name);
                        }
                        subrow[i] = StringUtil.IsExistAttr(subele.Attributes[i]);
                    }
                    dssubline.Rows.Add(subrow);
                }            linerow = dsline.NewRow();
                for (int i = 0; i < ele.Attributes.Count; i++)
                {
                    if (!dsline.Columns.Contains(ele.Attributes[i].Name))
                    {
                        dsline.Columns.Add(ele.Attributes[i].Name);
                    }
                    linerow[i] = StringUtil.IsExistAttr(ele.Attributes[i]);
                }
                dsline.Rows.Add(linerow);
            }
            dsmain.AcceptChanges();
            dsline.AcceptChanges();
            dssubline.AcceptChanges();
        }
    WebService里接受请求的方法:[WebMethod]
        public XmlDocument DoPost()
        {
            try
            {
                XmlDocument repdoc = new XmlDocument();
                XmlElement repRootEle = repdoc.CreateElement("Steel-ERP");
                repdoc.AppendChild(repRootEle);
                XmlElement repBodyEle = (XmlElement)repRootEle.AppendChild(repdoc.CreateElement("Body"));
                XmlElement repServerEle = (XmlElement)repBodyEle.AppendChild(repdoc.CreateElement("Server"));
                Stream requeststream = System.Web.HttpContext.Current.Request.InputStream;
                XmlDocument requestdom = new XmlDocument();
                //requestdom.Load(System.Web.HttpContext.Current.Server.MapPath("Config") + "\\UserInfo_Find.xml");
                requestdom.Load(requeststream);            XmlNode client = requestdom.GetElementsByTagName("Client")[0];            string job = client.Attributes["Job"].Value;
                ABizMgr abm = null;
                if (job.StartsWith("UserInfo_"))
                {
                    abm = new UserInfoMgr(job);
                    abm.ManagerVO((XmlElement)client, ref repServerEle, ref repdoc);
                }
                return repdoc;
            }
            catch (Exception e)
            {
                return null;
            }
        }
    然后调用的时候这么写:    protected void Button1_Click(object sender, EventArgs e)
        {
            dtmain.Clear();
            dtline.Clear();
            dtsubline.Clear();
            ActionUnit.FindMainID3("http://localhost:4942/WebSite4/WS_ES.asmx/DoPost", "UserInfo_Find", "4e043cc942e144f5823ba0481f465a16", ref dtmain, ref dtline, ref dtsubline);
            BindTOGV();
        }
    客户端通过XML与中间层交互.
    客户端发送一个这样格式的XML文件:<?xml version="1.0" encoding="gb2312"?>
    <Steel-ERP>
      <Head SessionID="681A4E9A140F42E4E044001321BD1C8B"/>
      <Body>
        <Client Job="UserInfo_Find">
          <Main Job="Find" ID="4e043cc942e144f5823ba0481f465a16"/>
        </Client>
      </Body>
    </Steel-ERP>
    中间层返回一个指定格式的XML文件.基本上跟楼主的需求差不多的,XML文件格式是要两边约定好的,否则无法通信.楼主可以试着改改``原理基本上就是这样了,代码改改,改成你自己的需求就可以了
      

  12.   

      XmlDocument doc = new XmlDocument();
            doc.Load("fileName");
            string form = string.Format("<form id='DynamicForm' action='xxx.aspx'><input type='hidden' value='{0}' /></form><script>document.getElementById('DynamicForm').submit();</script>", doc.OuterXml);
            Response.Write(form);用doc.OuterXml返回的只是内容,而其中的xml格式掉了,对方让我传整个格式都要,就传内容他们能解析么?
      

  13.   

    既然从文件中读,就不需要dom对象了。  System.IO.StreamReader sr = new System.IO.StreamReader("filename");
                string sXML = sr.ReadToEnd();
                sr.Close();
      

  14.   

    同样没有格式:只是以空格隔开的一些数据内容!
    =====>
    你不是说这个xml已经生成好了么?
      

  15.   

    是生成好了,在根目录下,我都看打开xml文件看了!
      

  16.   

    这个filename就是那个xml文件啊,读出来肯定是
    有格式的.
      

  17.   

    filename是Server .MapPath("ss.xml")
      

  18.   

    用webservice简单
    直接传个 字符串 参数就可以了
      

  19.   

    我也想用webservice,但是和移动做项目,他们不用这种方式,webservice给个方法然后把参数直接传过去,只适合,天气预报之类的应用!
      

  20.   

    14楼方法可以参考
    HttpWebRequest 和 responsestream
      

  21.   

    http://www.cnblogs.com/framework/archive/2007/02/24/654747.html
      

  22.   

     与目标url建立连接,然后以流的方式发送过去。使用WebClient类
    MSDN的例子:string uriString;
    Console.Write("\nPlease enter the URI to post data to : ");
    uriString = Console.ReadLine();
    Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
    string postData = Console.ReadLine();
    // Apply Ascii Encoding to obtain an array of bytes. 
    byte[] postArray = Encoding.ASCII.GetBytes(postData);// Create a new WebClient instance.
    WebClient myWebClient = new WebClient();// postStream implicitly sets HTTP POST as the request method.
    Console.WriteLine("Uploading to {0} ...",  uriString);                            Stream postStream = myWebClient.OpenWrite(uriString);postStream.Write(postArray,0,postArray.Length);// Close the stream and release resources.
    postStream.Close();Console.WriteLine("\nSuccessfully posted the data.");现在非常流行WinForm与web交互么?刚刚见一个帖子就是这个