0

I have a web service method and I convert to xml for viewing asp page my xml converter method:

        SqlDataAdapter MyDataAdapter;
        MyDataAdapter = new SqlDataAdapter(com.CommandText, SqlConnection);
        DataSet dsCategories = new DataSet();
        MyDataAdapter.SelectCommand.CommandTimeout = 1000;

        MyDataAdapter.Fill(dsCategories, "Category");
        dsCategories.WriteXml(Server.MapPath("Category.xml"));  

        System.IO.StringWriter writer = new System.IO.StringWriter();
        dsCategories.WriteXml(writer, XmlWriteMode.WriteSchema);
        string xmlFromDataTable = writer.ToString();
        response.Write(xmlFromDataTable);
        response.ContentType = "text/xml; charset=utf-16";

The error is

on line 1846 at column 19: XML declaration allowed only at the start of the document

The XML does not have the XML Declaration header. How can I fix it?

1 Answer 1

0

I suspect that there is some other stuff being written to the page which precedes your XML.

Perhaps if you try to following (add response.Clear()):

string xmlFromDataTable = writer.ToString();
response.Clear();
response.ContentType = "text/xml; charset=utf-16";
response.Write(xmlFromDataTable);

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.clear.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

thanks but it is no working i think, i find the error because of '&' sign. Because my data include more than '&' sign. How to fix it?
The ampersand you'd need to esacpe, but if your XML file is already well-formed then it should be escaped already. Can you just open the XML file on your filesystem with IE without any errors/warnings? This here pretty much covers anything that could go wrong. stackoverflow.com/questions/543319/how-to-return-xml-in-asp-net
XML file opened truely i dont understand the error but i approved another way, i use list for getting the data thanks for helping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.