www.RickLaFleur.com

 

Simple way to output XML in ASP.NET MVC

Another post to document something that I really don't want to have to look up ever again. I simply wanted to output XML to the browser window using ASP.NET MVC. Sounds easy, simply use:

public ContentResult Index()
{
    StringWriter writer = new StringWriter();
    myXmlDocument.Save(writer);
    return this.Content(writer.ToString(), @"text/xml", writer.Encoding);
}

But no luck; IE7's CSS would not display the XML since IIS ASP.NET defaults to UTF-16 and the previous page was UTF-8? Yes, both pages were correctly tagged with there encoding and correctly identified by IE as UTF-8 or UTF-16. It just wouldn't process the later. Whats up with that; can't these MS kids get along. So did a search and found a soluiton posted by Robert McLaw using a modified StringWriter that accepted an encoding which would worked very nicely:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;
public StringWriterWithEncoding(Encoding encoding)
{
  this.encoding = encoding;
}
public override Encoding Encoding
{
  get { return encoding; }
}
}

To implement, just use the new writer and set it's encoding as desired:

public ContentResult Index()
{
   StringWriterWithEncoding writer = new StringWriterWithEncoding(Encoding.UTF8);
   myXmlDocument.Save(writer);
   return this.Content(writer.ToString(), @"text/xml", writer.Encoding);
}

Labels:


 

Enterprise Search User Group

The user group aims to provide valuable and timely information to its members so as to foster the Microsoft Enterprise Search community’s growth locally in New York and worldwide via live broadcast presentations and recorded sessions. Its primary goal is to provide the opportunity and platform for otherwise disparate practices and business groups, to come together and share thoughts, ideas, successes and failures, in order to raise the bar - to increase quality across the board and grow a borderless body of knowledge. For details, please see: http://www.sharepointgroups.org/enterprisesearch/About.aspx

 

MET CS 601

MET CS 601 project, using "click relevency" for it's list, is still available here

Labels: , ,


 

Very simple wysiwyg console

I found this script at webmasterworld to write out HTML or JavaScript to an IFrame on the page; acts as a very simple wysiwyg console:

<html> 
<head> 
<title>HomeWork 1.0</title> 
</head> 
<body> 
<script type="text/javascript"><!-- 

function Txt2Frame() { 
document.frames.my_frame.document.open(); 
document.frames.my_frame.document.write('<html><head><title>My_Frame</title></head><body>'); 
document.frames.my_frame.document.write(Txt2Frame.arguments[0]); 
document.frames.my_frame.document.write('</body></html>'); 
document.frames.my_frame.document.close(); 
} 

//--></script> 

<form name="test_form"> 
<textarea onChange="Javascript:Txt2Frame(CharT.value);" name="CharT" cols="10" rows="5"></textarea> 
</form> 

<iframe frameborder="1" scrolling="0" name="my_frame" width="300" height="55"></iframe> 

</body> 
</html> 

Labels:


 

Simple method to download Web resource

Had to look this up - again - so writing a quick note for future reference.

public static string Download(string url)
{
    HttpWebRequest httpWebRequest =
      (HttpWebRequest)WebRequest.Create(url);

    HttpWebResponse httpWebResponse =
     (HttpWebResponse)httpWebRequest.GetResponse();

    Stream stream = httpWebResponse.GetResponseStream();

    StreamReader streamReader =
      new StreamReader(stream, Encoding.ASCII);

    return streamReader.ReadToEnd();
}

Labels:


Archives

January 2007  |   March 2007  |   May 2007  |   February 2008  |   April 2008  |   July 2008  |   September 2008  |   October 2008  |   December 2008  |   January 2009  |   April 2009  |   May 2009  |   June 2009  |   December 2009  |   January 2010  |  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]