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:


 

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:


 

Setting the property for div in a ContentPlaceHolder

Following shows how to set the property for a div that is in a C#.NET ContentPlaceHolder found with Master Pages:

Step 1; in the aspx file, the div needs a unqiue id and set to runat="server":


<asp:Content ID="conent1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <div id="myDivId" runat="server" style="display: block;">
    <p>foo bar</p>
  </div>
</asp:Content>

Step 2; In the aspx.cs file, the ContentPlaceHolderId is retrieved first using the Page.Master.FindControl and then used to reference the div:

ContentPlaceHolder content = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");
content.FindControl("myDivId").Visible = false;

Labels:


 

Setting the Style for div via C#.NET

Had to look this up again so documenting for quick reference. It's rather simple to set a div style using the following steps:

Step 1; The div must have a unique id and be configured with runat="server":

<div id="myDivId" runat="server" class="head2">

Step 2; The code behind references the div as if it were any other Control on the page, using the Controls Style property to set a style attribute:

myDivId.Style["background-color"] = "#ffdd77";

Labels:


 

csharp switch statement


for (int i = 1; i < 10; i++)
 switch (i)
 {
  case 1:
  case 2:
  case 3: 
   Console.WriteLine("i is 1, 2 or 3");
   break;
  case 4: 
   Console.WriteLine("i is 4");
   break;
  default:
   Console.WriteLine("i not 1, 2, 3, or 4");
   break;
 } 
}

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]