CSharp - 在 C# 中,将XML文档作为参数传递给 Web服务

  显示原文与译文双语对照的内容
0 0

我必须将XML文档作为参数发送,以便使用Post方法从服务请求 web request 。

谁可以帮助将XML文档作为参数发送,或者如何获取字符串中的整个文档以作为文档传递。

时间: 原作者:

0 0

如果要使用命名表单参数发布Xml数据,需要执行如下操作:


HttpWebRequest request = HttpWebRequest.Create("http://yourdomain.com/whatever") as HttpWebRequest;
request.Method ="POST";
request.ContentType ="application/x-www-form-urlencoded";

Encoding e = Encoding.GetEncoding("iso-8859-1");
XmlDocument doc = new XmlDocument();
doc.LoadXml("<foo><bar>baz</bar></foo>");
string rawXml = doc.OuterXml;

//you need to encode your Xml before you assign it to your parameter
//the POST parameter name is myxmldata
string requestText = string.Format("myxmldata={0}", HttpUtility.UrlEncode(rawXml, e));

Stream requestStream = request.GetRequestStream();
StreamWriter requestWriter = new StreamWriter(requestStream, e);
requestWriter.Write(requestText);
requestWriter.Close();

原作者:
0 0

阅读本文介绍的XML文档和web服务传递XML文档作为 Web服务的参数。


 [WebMethod]

public System.Xml.XmlDocument SampelXmlMethod( System.Xml.XmlDocument xmldoc)


 string xmldata ="<xform>" +

"<instance>" +

"<FirstName>Andrew</FirstName>" +

"<LastName>Fuller</LastName>" +

"<BirthDate>2/19/1952</BirthDate>" +

"</instance>" +

"</xform>";



//Load xmldata into XmlDocument Object
 System.Xml.XmlDocument SendingXmlDoc = new System.Xml.XmlDocument();

 SendingXmlDoc.LoadXml(xmldata);



//Call web service and get xmldocument back 
 System.Xml.XmlDocument ReceivingXmlDoc = new System.Xml.XmlDocument();

 XmlService ser = new XmlService();//Your web srevice..

 ReceivingXmlDoc = ser.SampelXmlMethod(SendingXmlDoc);

...