Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error in displaying XML string using C#

Status
Not open for further replies.

Dhinga

Programmer
Jun 15, 2006
9
US
Hi,

I need some help.

I am retrieving a result set from the database & storing it in into a Datatable using C#. I am then using the WriteXML method of the Datatable to convert it into XML string & then I am writing the XML string to the page after setting the content type of the page to "text/xml". When I run my page for the first time I get the error


The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

------------------------------------------------------------
Only one top level element is allowed in an XML document.



When I refresh the page this error goes away & it shows me the correct XML string.

Following is the code that I am using after getting the result in the Datatable:


string strRateXML = "";
System.IO.StringWriter sw = new System.IO.StringWriter();
DataTable dtCurrent = clsBTSql.GetDataTable();

dtCurrent.TableName = "Rates";

dtCurrent.WriteXml(sw);

strRateXML = sw.ToString();

Response.Clear();
Response.ContentType = "text/xml";
Response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>");
Response.Write(strRateXML);
Response.Flush();
Response.End();



Can anybody please tell me what am I doing wrong here?
 
No I don't have any HTML on my ASPX page. My ASPX page only has Page directive.

Thanks
 
OK, can you provide the contents of "sw.ToString()"?

Also, check to make sure there are no empty lines at the top of the rendered xml file as sometimes that causes a problem.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I am pasting the contents of sw.ToString() here without any formatting.

<DocumentElement>\r\n <Rates>\r\n <FundName>ISJIT DIV</FundName>\r\n <Yield>4.9697</Yield>\r\n <YieldDate>04/25/2007</YieldDate>\r\n </Rates>\r\n <Rates>\r\n <FundName>ISJIT DGO</FundName>\r\n <Yield>4.8855</Yield>\r\n <YieldDate>04/25/2007</YieldDate>\r\n </Rates>\r\n <Rates>\r\n <FundName>ISJIT DIVP</FundName>\r\n <Yield>4.9676</Yield>\r\n <YieldDate>04/25/2007</YieldDate>\r\n </Rates>\r\n</DocumentElement>
 
I can't replicate your error. My page:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default36.aspx.vb" Inherits="Default36" %>
with this code (I'm assuming the "\r\n" text wasn't supposed to be in there?) :
Code:
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Clear()
        Response.ContentType = "text/xml"
        Response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>")
        Response.Write("<DocumentElement><Rates><FundName>ISJIT DIV</FundName><Yield>4.9697</Yield><YieldDate>04/25/2007</YieldDate></Rates><Rates><FundName>ISJIT DGO</FundName><Yield>4.8855</Yield><YieldDate>04/25/2007</YieldDate></Rates><Rates><FundName>ISJIT DIVP</FundName><Yield>4.9676</Yield><YieldDate>04/25/2007</YieldDate></Rates></DocumentElement>")
        Response.Flush()
        Response.End()
    End Sub
It's going to be quite hard to help you without being able to replicate the error, so I'd suggest starting a new test page and performing a test like I've done. This will at least determine whether there is a problem with the relevant page or the xml.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top