0

我有一个 Web 服务方法,我转换为 xml 以查看我的 xml 转换器方法的 asp 页面:

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

错误是

在第 1846 行第 19 列:XML 声明只允许在文档的开头

XML 没有 XML 声明标头。我该如何解决?

4

1 回答 1

0

我怀疑在您的 XML 之前的页面中写入了一些其他内容。

也许如果您尝试关注(添加 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

于 2013-05-07T12:54:35.713 回答