none
Read Exact XML Sent By Client - WCF REST Method RRS feed

  • Question

  • I want to read the exact XML sent by client for my WCF REST Method, I have following lines of code 

    public YDepartmentResponse GetDepartment(YDepartmentRequest rData)
            {

    string incomingXML = OperationContext.Current.RequestContext.RequestMessage.ToString();

    }

    There are newline chars & spaces between XML tags, my caller is not passing those.

    Currently I am removing these by using String.Replace

    incomingXML = incomingXML .Replace(Environment.NewLine, "");
    incomingXML = incomingXML .Replace(">    <", "><");
    incomingXML = incomingXML .Replace(">  <", "><");

    Is there a way I can read exact XML sent by caller without /r/n & spaces ?

    Thanks in advance !!!

    Saurabh Sondhi

    Wednesday, May 29, 2013 10:07 AM

Answers

  • How about loading the xml-string into and XmlDocument?

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml("<test>   <test2></test2> </test>");

    Then you should be able to use xmlDoc.OuterXml and get the XML without spaces and /r/n.


    • Edited by johnviggo Wednesday, May 29, 2013 10:25 AM
    • Proposed as answer by Nadim Sayyed Wednesday, May 29, 2013 11:32 AM
    • Marked as answer by Saurabh Sondhi Wednesday, May 29, 2013 11:49 AM
    Wednesday, May 29, 2013 10:25 AM

All replies

  • How about loading the xml-string into and XmlDocument?

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml("<test>   <test2></test2> </test>");

    Then you should be able to use xmlDoc.OuterXml and get the XML without spaces and /r/n.


    • Edited by johnviggo Wednesday, May 29, 2013 10:25 AM
    • Proposed as answer by Nadim Sayyed Wednesday, May 29, 2013 11:32 AM
    • Marked as answer by Saurabh Sondhi Wednesday, May 29, 2013 11:49 AM
    Wednesday, May 29, 2013 10:25 AM
  • Hi Saurabh,

    Your code will look like this:

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(incomingXML);

    and if you want to use  you can add

    string xml = doc.OuterXml; 

    Hope this helps.


    MCTS- Please vote and mark posts as answered where appropriate.

    • Proposed as answer by Nadim Sayyed Wednesday, May 29, 2013 11:43 AM
    Wednesday, May 29, 2013 11:40 AM
  • It worked, Thanks so much !!!
    Wednesday, May 29, 2013 11:49 AM