writing XML to string to pass to web method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve K.

    writing XML to string to pass to web method

    I need to generate in memory xml to pass to a web service method. VS did
    not generate proxies for the objects that are needed for these web methods.
    I'm spoiled, I've used many web services but never really needed to know how
    to transmit the request as XML.

    Here is the code I have to create the XML:

    System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
    XmlTextWriter w = new XmlTextWriter(s w);
    w.Formatting = Formatting.Inde nted;
    w.WriteStartDoc ument();
    w.WriteStartEle ment("RefundReq uest");
    w.WriteStartEle ment("AccountID ");
    w.WriteValue(AC COUNT_ID);
    w.WriteEndEleme nt();
    w.WriteStartEle ment("WebPasswo rd");
    w.WriteValue(AC COUNT_PASSWORD) ;
    w.WriteEndEleme nt();
    w.WriteStartEle ment("RefundLis t");
    w.WriteStartEle ment("PICNumber ");
    w.WriteValue(tr ackingNumber);
    w.WriteEndEleme nt();
    w.WriteEndEleme nt();
    w.WriteEndEleme nt();
    w.WriteEndDocum ent();
    w.Close();

    string requestXml = sw.ToString();


    I have decorated the web methods in references.cs with the TraceExtension
    attribute that will print the soap request/response to a log file.
    Here is what the above XML is logged as:

    ----SoapRequest at 9/19/2008 10:40:19 PM
    <?xml version="1.0" encoding="utf-8"?><soap:Envel ope
    xmlns:soap="htt p://schemas.xmlsoap .org/soap/envelope/"
    xmlns:soapenc=" http://schemas.xmlsoap .org/soap/encoding/"
    xmlns:tns="http ://ELS" xmlns:types="ht tp://ELS/encodedTypes"
    xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http ://www.w3.org/2001/XMLSchema"><soa p:Body
    soap:encodingSt yle="http://schemas.xmlsoap .org/soap/encoding/"><tns:RefundRe quest><XMLInput
    xsi:type="xsd:s tring">&lt;?xml version="1.0" encoding="utf-16"?&gt;
    &lt;RefundReque st&gt;
    &lt;AccountID&g t;659995&lt;/AccountID&gt;
    &lt;WebPassword &gt;K!llerB00ts !&lt;/WebPassword&gt;
    &lt;RefundList& gt;
    &lt;PICNumber&g t;9122148008600 123456781&lt;/PICNumber&gt;
    &lt;/RefundList&gt;
    &lt;/RefundRequest&g t;</XMLInput></tns:RefundReque st></soap:Body></soap:Envelope>

    I realize there are several problems here, but the first one I would like to
    tackle is the '<' and '>' characters being replaced. I encountered this
    when I first tried to write the XML to a string. I then googled and read a
    bit about escaping XML and the suggestion was to do what I've done above.
    However it seems that when I write the XML to the StringWriter it's
    "unencoded" again or "unescaped" - remember, I don't know what I'm talking
    about! ;0)

    Can anyone clue me in on what I'm missing here? I'd love to spend days
    learning about this (I would, really) but like most of you I'm in a time
    crunch. I need a quick fix, I will research and learn why later.

    Any help greatly appreciated,
    Steve


Working...