Return xml file as string in C#.net

I am trying to convert xml file as a string, so that I can pass this xml as a string to the web services. While passing this string(xml) to webservices, I am getting following error :

Client found response content type of ‘text/html; charset=UTF-8’, but expected ‘text/xml’.

here is the code :

public void cRoom(int id,string updatedXmlPath)
{

CadService ceserviceSend = new CadService();

          string  updatedXml = getXmlAsString(updatedXmlPath);
            ceserviceSend.CompleteRoom(id, updatedXml, true);
    }

public string getXmlAsString(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
try
{

            xmlDoc.Load(xmlFilePath);
            StringWriter sw = new StringWriter();
            XmlTextWriter xw = new XmlTextWriter(sw);
            xmlDoc.WriteTo(xw);
            return sw.ToString();

        }

 I will have to set up content-type as "text/xml". However, I don't know

how to do that when you use StringWriter. Any help would be greately appreciated.

-Thanks

You really should be passing XmlDocuments around rather than strings that happen to contain XML. But I digress.

In any case, what you want is the XmlDocument.OuterXml property to get the xml string.

Is there any way I can set content-type to “text/xml” using xmltextwriter?

You need to set that using the response object, not the XmlTextWriter.

Sample code for writing Xml:


XmlDocument doc=new XmlDocument();
doc.Load(filename);
Response.ContentType="text/xml";
Response.Write(doc.OuterXml);

I don’t have much experience in .net. This code is in a console application. Will this Response.write will work? Thank you for the help!

Oh. Then the content type is immaterial; it only really applies to web pages.

Actually I am passing this xml string (sw.tostring()) to the web services and this webservices is not accepting it, giving the following message :

Client found response content type of ‘text/html; charset=UTF-8’, but expected ‘text/xml’.

I suspect you do not have the Url correct for the web service. It is getting an Html response rather (eg: the description page or an error message) rather than the expected Xml response.

Dont ever use Response.Write, rather use a “writer” in any form neccesary, try and use an instance of the xmlDocument, it’s there exacly for this problem.

Are you sure your service works well, you have a link to the asmx file to see whether your methods are reacheable and ask for the right parameters…

Another thing, you should try access your URl’s anytime through the Uri class and use that to point to certain services i.e. asmx files

U use VS2005 ?, tries the “Add Web Service” yet, if your methods come up correct there there should be no problem in using them

Thanks to you all. It seems there is problem with the web services, which was developed in ruby on rails.