Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
My project have to act like that: http://xml.alexa.com/data?cli=10&dat=nsa&url=gomm.com.vn[^]

The link with querystring return an xml content.
How can I do that?
Anyone can give me some guideline, please, I've googled but not found anything.
Thanks in advance,
TuanNM
Posted
Updated 10-Aug-11 3:42am
v2

Set Response.ContentType = "application/xml" and write your xml to the output stream.
 
Share this answer
 
Generate the xml, write it to the response output stream and set the response content type to "text/xml" or "application/xml".
 
Share this answer
 
Hi,

I have achieved the same solution using the code mentioned below.

under page load write below code.

C#
protected void Page_Load(object sender, EventArgs e)
    {



        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";


            GenerateXML("XML_Output", Response.OutputStream);

        Response.End();


    }



and for GenerateXML method write below code

C#
public void GenerateXML(string Format, Stream stream)
    {
        XmlWriterSettings settings = new XmlWriterSettings();


        settings.Indent = true;
        using (XmlWriter writer = XmlWriter.Create(stream, settings))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("root");

            //Get values from the database into a datatable


            foreach (DataRow dr in extracteddatatable.Rows)
            {
                // Repeat this code:
                string strDes = "<![CDATA[Class : <b>"+ "]]>";

                writer.WriteStartElement("rootelement");

                writer.WriteElementString("RLS ", dr["extractedfield"].ToString());



                //end location subroot
                writer.WriteEndElement();


            }




            writer.WriteEndElement();
            writer.WriteEndDocument();
        }
    }


and don't forget to add below mentioned namespaces :)
C#
using System.Xml.Xsl;
using System.Xml.Schema;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;


please let me know if any problem
 
Share this answer
 
Comments
TuanNGUYEN 12-Sep-11 4:09am    
Thanks for your detail answer.
I still program back-end modules, if i have problem when try your example, i will ask you more.
I think that you need to do it with Ajax. When making a server request in Ajax, the data returned can be in either plain text/html, or an XML document instead. The later is technically just a text file as well, but with some special instructions, Ajax can retrieve that well formed XML text file and return it back to you as a XML object. This enables the XML data to be easily parsed using standard DOM methods.

Here's a simple XML document. It is showed in RSS format I'll be using for illustration (lets name it "results.xml"):

HTML
<rss version="0.91">
<channel>
<title>domain.com</title>
<link>http://www.domain.com</link>
<description>free scripts showing XML document</description>
<language>en</language>

<item>
<title>Document Text Resizer</title>
<link>http://www.domain.com/script/script2/doctextresizer.shtml</link>
<description>This script adds the ability for your users to toggle your webpage's font size, with persistent cookies then used to remember the setting</description>
</item>

<item>
<title>JavaScript Reference- Keyboard/ Mouse Buttons Events</title>
<link>http://www.domain.com/jsref/eventkeyboardmouse.shtml</link>
<description>The latest update to our JS Reference takes a hard look at keyboard and mouse button events in JavaScript, including the unicode value of each key.</description>
</item>

<item>
<title>Dynamically loading an external JavaScript or CSS file</title>
<link>http://www.domain.com/javatutors/loadjavascriptcss.shtml</link>
<description>External JavaScript or CSS files do not always have to be synchronously loaded as part of the page, but dynamically as well. In this tutorial, see how.</description>
</item>

</channel>
</rss>


The below shows retrieving this XML document and outputing the headlines ("title" elements) of each entry:



JavaScript
<script type="text/javascript">

function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length;>   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
    //suppress error
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else
  return false
}

var mygetrequest=new ajaxRequest()
if (mygetrequest.overrideMimeType)
 mygetrequest.overrideMimeType('text/xml')
mygetrequest.onreadystatechange=function(){
 if (mygetrequest.readyState==4){
  if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
   var xmldata=mygetrequest.responseXML //retrieve result as an XML object
   var rssentries=xmldata.getElementsByTagName("item")
   var output='<ul>'
   for (var i=0; i<rssentries.length;>    output+='<li>'
    output+='<a href="'+rssentries[i].getElementsByTagName('link')[0].firstChild.nodeValue+'">'
    output+=rssentries[i].getElementsByTagName('title')[0].firstChild.nodeValue+'</a>'
    output+='</li>'
   }
   output+='</ul>'
   document.getElementById("result").innerHTML=output
  }
  else{
   alert("An error has occured making the request")
  }
 }
}

mygetrequest.open("GET", "results.xml", true)
mygetrequest.send(null)

</script>


I hope that it will be useful
 
Share this answer
 
Comments
[no name] 10-Aug-11 21:19pm    
Ajax is not necessary, and besides this is horrible code. You should research xslt and JQuery

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900