Webservice method returns XML.

Discussion in 'ASP .Net Web Services' started by George Ter-Saakov, Dec 4, 2003.

  1. I wrote webservice with one method Test.

    When i am trying to return a simple string it works form test client.
    When i am trying to return an XML as a string the client throws an error.

    "An unhandled exception of type 'System.InvalidOperationException' occurred
    in system.xml.dll"

    When i test service with a browser it works. Test2 returns an XML.


    Here is the code i use for WebService (Test2 works, Test1 does not).

    [WebMethod]
    public string Test2(string sBod)
    {
    return "Hello from ShipmentStatus";
    }

    [WebMethod]
    public string Test1(string sBod)
    {
    return GetXml("Hello from ShipmentStatus");
    }

    public string GetXml( string sXml)
    {
    MemoryStream mem = new MemoryStream();
    XmlTextWriter xml = new XmlTextWriter(mem,
    System.Text.UTF8Encoding.UTF8);
    xml.Formatting = Formatting.Indented;
    xml.WriteStartDocument();
    xml.WriteElementString("TEST", sXml);
    xml.WriteEndDocument();
    xml.Close();
    byte[] buf = mem.GetBuffer();
    return System.Text.UTF8Encoding.UTF8.GetString(buf,0, buf.Length);
    }


    -----Client code ---

    Service1 serv = new Service1();
    string sreply1 = serv.Test1("sadf");

    string sreply2 = serv.Test2("sadf");

    -----------------------------------------

    Thanks.
    George.
     
    George Ter-Saakov, Dec 4, 2003
    #1
    1. Advertising

  2. Hi,

    I can't make out the exact error, but its certainly not with the Web
    Service. What seems to be a problem is the way you are writing to the
    MemoryStream. I would suggest you write to a StringWriter stream object. and
    make sure you close the streams once you finish working with them.

    Regards,
    Saurabh Nandu
     
    Saurabh Nandu, Dec 5, 2003
    #2
    1. Advertising

  3. George Ter-Saakov

    MSFT Guest

    Hi George,

    The problem should be ralated to following line:

    return System.Text.UTF8Encoding.UTF8.GetString(buf,0, buf.Length);

    buf.Length is 256 here and it is not an actual value for the string. After
    I change it to:

    return System.Text.UTF8Encoding.UTF8.GetString(buf,0, 81);

    It seems to work.

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Dec 5, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Turner
    Replies:
    2
    Views:
    9,198
    Ken Cox [Microsoft MVP]
    Jan 2, 2004
  2. vizlab
    Replies:
    3
    Views:
    4,234
    Michael Bar-Sinai
    Oct 17, 2007
  3. Pim75
    Replies:
    1
    Views:
    566
    Cowboy \(Gregory A. Beamer\)
    Mar 21, 2007
  4. Michael Haberfellner

    Webservice returns recordset as XML-File

    Michael Haberfellner, Aug 9, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    345
    =?Utf-8?B?Y3NoYXJwZXI=?=
    Aug 9, 2007
  5. René Preußler

    Asynchronous Call of WebService method within a WebService

    René Preußler, Oct 29, 2008, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    205
    René Preußler
    Oct 29, 2008
Loading...

Share This Page