Display Source Code - With Formatting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmk
    New Member
    • Aug 2007
    • 5

    Display Source Code - With Formatting

    Hi Again,

    I am reading a document into a page and then displaying it as source code (this is for an assignment.)

    I feel that the output that I am getting is a little hard to read, and want to go beyond the scope of the assignment. I want to add formatting (just like what the [ CODE ] tags on this forum does.

    Does anyone know of a simple to implement API for ASP.NET that will allow me to implement this using the method of input below?

    [CODE=c]<script language="C#" runat="server">
    void Page_Load(objec t sender, System.EventArg s e)
    {
    bool blnEmpty = (Request.QueryS tring["page"] == null || Request.QuerySt ring["page"].Length == 0);
    if(!blnEmpty){
    string strDocument = Request.QuerySt ring["page"];

    lblDocumentName .Text = strDocument;

    string strPath = Server.MapPath( strDocument);

    string strSourceCode = "";

    try
    {
    strSourceCode = "";
    // Create an instance of StreamReader to read from a file.
    // The using statement also closes the StreamReader.
    using (System.IO.Stre amReader sr = new System.IO.Strea mReader(strPath ))
    {
    string line;
    // Read and display lines from the file until the end of
    // the file is reached.
    while ((line = sr.ReadLine()) != null)
    {
    strSourceCode += line.Replace("< ", "&lt;").Replace (">", "&gt;") + "<br/>";
    }

    //strSourceCode = strSourceCode.R eplace("<", "&lt;").Replace (">", "&gt;");
    lblSourceCode.T ext = "<pre>" + strSourceCode + "</pre>";
    }
    }
    catch (Exception ex)
    {
    // Let the user know what went wrong.
    lblSourceCode.T ext = "The Document could not be read:<br/>";
    lblSourceCode.T ext += ex.Message;
    }
    } else {
    lblDocumentName .Text = "None";
    lblSourceCode.T ext = "No Document Specified.";
    }
    }
    </script>[/CODE]

    I'm using the CSS below to make sure that the wrapping works as expected:

    [CODE=css]pre {
    white-space: pre-wrap; /* css-3 */
    white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
    white-space: -pre-wrap; /* Opera 4-6 */
    white-space: -o-pre-wrap; /* Opera 7 */
    word-wrap: break-word; /* Internet Explorer 5.5+ */
    }[/CODE]

    Thanks
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Way to go man, I know that you will ace this assignment with a little help from Microsoft:
    ASP.NET Configuration API

    If only I had thought to go beyond scope on my assignments instead of wasting time chasing girls and playing sports, then I might not have ended as a lowly web developer. My advice study hard and take up karate to fend off those jocks.

    Comment

    • dmk
      New Member
      • Aug 2007
      • 5

      #3
      Originally posted by kenobewan
      Way to go man, I know that you will ace this assignment with a little help from Microsoft:
      ASP.NET Configuration API

      If only I had thought to go beyond scope on my assignments instead of wasting time chasing girls and playing sports, then I might not have ended as a lowly web developer. My advice study hard and take up karate to fend off those jocks.
      Only reason I want to do this is because I have a job for after uni already... I just don't know the language (C#) that they want me to work with ;) might as well get the learning out of the way now.

      Thanks for the help, not really what I was looking for... i've only got a couple of weeks for the assignment, and I'm not going THAT far out of my way ;)

      Comment

      Working...