9

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.

3
  • The tag web-services is a little misleading here. You're not consuming a WebService but rather posting Xml data like Form data. Commented Feb 4, 2011 at 9:33
  • Did you try to send XmlDocument.OuterXml (which is of type string)? Commented Feb 4, 2011 at 9:49
  • What is OuterXML is this any inbuit function? I thing this is not for XMLDocument but it is for XmlNode. Commented Feb 4, 2011 at 9:52

2 Answers 2

8

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Filburt, But u have the XMLDocument can you specify my about how to convert XMLDocument to string?
@Bhavik Like EvgK suggested you can get the Xml string by XmlDocument.OuterXml property.
Sorry Sir, But there is no such property XmlDocument.OuterXml instead it is XmlNode.OuterXml.
5

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);

1 Comment

Sorry but i have to get this thru the POST method as i cannot pass this as an argument to the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.