views:

122

answers:

4

I've been doing ASP.NET a little bit (on and off) over the last year, but I've never come upon this challenge: I'm building a website right now which is quite simple, mostly based in HTML and Javascript. However, on one page, I need to read an XML file off the server, parse it, create HTML from values contained in the XML file, and output it as the response. I am going to use ASP.NET with C# for this. I understand how to parse the XML and generate the HTML code in C#, but how do I write the HTML code into the response/into the page? The generated dynamic HTML is only in one big div in the page, and the rest of the page is static. What is the best way to do this? As I've never done anything like this before, I'm guessing that one way to do it would be to clear the whole HTML source of the page and use Response.Write() in the Page_Load event to write in the whole HTML of the page, with the XML values already inserted. Is this the correct method, and if so, could you give me a few lines of code as an example to make sure that I'm doing it right? Thanks!

Also, as I've never had the opportunity to do this before, what is the best way of reading a file in ASP.NET C# that is located on your server?

UPDATE: Thank you for all the answers! I have found the solution to my problem, and yet all three answers provided are good ways of approaching this challenge. As you can guess, it's a tough choice who to give the accepted answer to, but I'm going to give it to this answer, by awe, because he clearly put a lot of effort into it, it's a quite elegant solution, and he answered both my questions. Thank you all for the wonderful answers!

+4  A: 

In codebehind function:

public string getHML()
{
 return "htmltext";
}

on Page:

<div><%=getHML()%></div>
x2
So getHML() will be called when the page is loading? Cool! Going to try it!
Maxim Zaslavsky
I really love how elegant this method is, as I never knew that you can embed server-side code in HTML like that. Thanks so much! I'm using this!
Maxim Zaslavsky
+1  A: 

Well, your own suggestion would certainly work. Clear out all the html in the ASPX page, and in the Page_Load event you'll do this:

Response.Write(System.IO.File.ReadAllText(yourFilePath));

I don't think there's much more to it.

Jakob Gade
Thank you for your answer.
Maxim Zaslavsky
You're welcome. :)
Jakob Gade
+4  A: 

Create a div that is accessible in server code:

<div runat="server" id="xmlGeneratedContent"></div>

In Page_Load:

xmlGeneratedContent.InnerHtml = parcedHtmlFromXml;

EDIT:
As answer to the last question: how to read a file on the server...
If the file is located under the web site, you can use Server.MapPath to get the physical disk location from the relative url:

string filename = Server.MapPath("files/file.txt");

How to read it depends on what kind of file it is, and how you want to read it. If you want to read it as plain text, here are some methods:

Read all at once:

string content = System.IO.File.ReadAllText(filename);

Read all at once into string array containing the lines:

string[] content = System.IO.File.ReadAllLines(filename);

Read one line at a time:

System.IO.StreamReader sr = new System.IO.StreamReader(filename);
while (!sr.EndOfStream)
{
    string line = sr.ReadLine(); // or other method reading a block 
    //Do something whith the line
}
sr.Close();
sr.Dispose();
awe
I favor this as it is way cleaner.
Dykam
I have updated the OP to explain why I have marked this as accepted answer. Thanks!
Maxim Zaslavsky
I think you might want to check out the asp.net lifecycle <http://msdn.microsoft.com/en-us/library/ms178472.aspx> to ensure you don´t do this wrong. If you use post and not get I think you might want to do the above in page_init...
zzzuperfly
the link got f** up : http://msdn.microsoft.com/en-us/library/ms178472.aspx
zzzuperfly
+2  A: 

Just to add to the diversity: My favorite solution is to use

<asp:Literal runat="server" ID="myLiteral" />

And then in code:

this.MyLiteral.Text = "Generated HTML goes here";

The advantage over a <div> is that this generates no extra HTML - so you can put it wherever you want and generate whatever you want.

Often I also set EnableViewState="false" on it, if I can easily regenerate the contents on every request. This cuts down on the ViewState size, because the myLiteral.Text is also saved in ViewState.

Vilx-
I'd +1 you for mentioning viewstate, but can't be bothered registering ;)
wefwfwefwe