Professional Web Applications Themes

Webservice method returns XML. - ASP.NET Web Services

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) { ...

  1. #1

    Default Webservice method returns XML.

    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.WriteStartDoent();
    xml.WriteElementString("TEST", sXml);
    xml.WriteEndDoent();
    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 Guest

  2. #2

    Default Re: Webservice method returns XML.

    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 Guest

  3. #3

    Default RE: Webservice method returns XML.

    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! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    MSFT Guest

Similar Threads

  1. FindControl Method always returns empty string!!!!!
    By steroche in forum ASP.NET Building Controls
    Replies: 5
    Last Post: August 25th, 03:15 AM
  2. Replies: 3
    Last Post: August 8th, 05:57 PM
  3. XmlDoent Web method returns type XmlNode
    By John Bailo in forum ASP.NET Web Services
    Replies: 3
    Last Post: November 15th, 11:56 PM
  4. How to consume a Web Method that returns a custom class?
    By Christopher Ambler in forum ASP.NET Web Services
    Replies: 0
    Last Post: September 26th, 09:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139