Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing XML document as an parameter to Web services in C#

Tags:

c#

I have to sent the XML document as an parameter to request an WebRequest from the Service using the Post method.

Can anyone help be about how to sent the XML document as an parameter, or how to get the whole document in the string to pass as in as Document.

like image 541
Bhavik Goyal Avatar asked Feb 04 '11 09:02

Bhavik Goyal


2 Answers

If you want to POST your Xml data using a named form parameter you need to do something like this:

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();
like image 51
Filburt Avatar answered Nov 04 '22 19:11

Filburt


Read this article Which is explained about the XML document and web service Passing XML document as an parameter to Web services

  [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);
like image 22
Shashi Avatar answered Nov 04 '22 20:11

Shashi