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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling ASPX File and Getting XML Data Back... 1

Status
Not open for further replies.

wbochar

IS-IT--Management
Mar 14, 2003
72
US

I am working on an app that needs to call and aspx page and that page needs to return raw xml data/text.

So calling on xmldata.aspx would return a DB dump in raw text xml.

This is the code so far, but I do not know how to make the xml return as pure xml, not xml text dumped into and html page. Something to do with setting the page encoding?

here is my code so far:

Code:
private void Page_Load(object sender, System.EventArgs e)
{
	String strConnect;
	String strCommand;
	strConnect = @"Data Source=DEV;Initial Catalog=CORP;Persist Security Info=True;Packet Size=4096";
	strCommand = "SELECT TOP 5 * FROM ARTICLE WHERE ARTICLE_LANGUAGE='EN'";
	SqlConnection myConnection_Groups = new SqlConnection(strConnect);
	SqlCommand myCommand_Groups = new SqlCommand(strCommand,myConnection_Groups);
	myConnection_Groups.Open();
	SqlDataReader myReader_Groups;
	myReader_Groups = myCommand_Groups.ExecuteReader();
			
	// XML Document Startup before Loop
	XmlDocument xmlDoc = new XmlDocument();
	XmlDeclaration declarer = xmlDoc.CreateXmlDeclaration("1.0","UTF-8","yes");
	xmlDoc.AppendChild(declarer);
	// Root Structure
	XmlElement article_list = xmlDoc.CreateElement("article_list");
        XmlElement article, title,description,body;
	XmlText textNode;
			
	while (myReader_Groups.Read())
	{
	//Add an Article to the XML
	article = xmlDoc.CreateElement("article");
	article.SetAttribute("id", myReader_Groups["ARTICLE_ID"].ToString());
	article.SetAttribute("title_colour","#FFFFFF");
	article.SetAttribute("language",myReader_Groups["ARTICLE_LANGUAGE"].ToString());
	article.SetAttribute("image","Car.jpg");
	article_list.AppendChild(article);
	title = xmlDoc.CreateElement("title");
	description = xmlDoc.CreateElement("description");
	body = xmlDoc.CreateElement("body");
	textNode = xmlDoc.CreateTextNode("titletext");
        textNode.Value = myReader_Groups["ARTICLE_TITLE"].ToString();
	article.AppendChild(title);
	title.AppendChild(textNode);
	textNode = xmlDoc.CreateTextNode("descriptiontext");
	textNode.Value = myReader_Groups["ARTICLE_DESCRIPTION"].ToString();
	article.AppendChild(description);
	description.AppendChild(textNode);
	}

	// always call Close when done reading.
	myReader_Groups.Close();
	// Close the connection when done with it.
	myReader_Groups.Close();

	//Dump XML to the Text box
	xmldata.Text=article_list.OuterXml;
}

I am dumping the xml output for now into a textbox to debug the output. I noticed the output is ok, but I dont see the preamble showing up either <?xml version="1.0" ?>..

anyone have any suggestions on how to dump only an XML chunk as opposed to a aspx html with xml inside it.

 
Is there a reason why you wouldn't want to move the XML generation to a class file?

Either way, here you go:

Code:
[URL unfurl="true"]http://aspnet.4guysfromrolla.com/articles/092403-1.aspx[/URL]

[b]<@ Page ContentType="text/xml" %>[/b]
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Text" %>
<script language="C#" runat="server">
void Page_Load(object sender, EventArgs e)
{
  // Create a new XmlTextWriter instance
  XmlTextWriter writer = new 
       XmlTextWriter(Response.OutputStream, Encoding.UTF8);
    
  // start writing!
  ...
}
 
Thanks! I read the article you linked/ref'd and it was extremely helpful. In response to your question; It's already there, I was just using a test setup to get everything working. Once it was going the way I wanted it to -- I would move it over.

--Wolf

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top