Webservice method returns XML.

  • Thread starter George Ter-Saakov
  • Start date
G

George Ter-Saakov

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

Saurabh Nandu

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
 
M

MSFT

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top