ASP.NET:动态地生成HTML,怎么样? [英] ASP.NET: dynamicly generating HTML, how to?

查看:101
本文介绍了ASP.NET:动态地生成HTML,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做ASP.NET一点点(或关闭)在过去的一年,但我从来没有临到这个挑战:我建立一个网站,现在这是很简单的,主要是基于在HTML和Javascript。然而,在一个页面上,我需要读取XML文件关闭服务器,解析它,创建从包含在XML文件中的值的HTML,和输出它作为响应。我将使用ASP.NET与C#这一点。我知道如何解析XML并生成C#中的HTML code,但我怎么写HTML code到响应/到页面?所产生的动态HTML仅在页面一个大格,并且页面的其余部分是静止的。什么是做到这一点的最好方法是什么?正如我从来没有做过这样的事之前,我猜这样做的一种方式它会清除页面的HTML全源和在Page_Load事件中使用的Response.Write()在整个HTML写的页面,与XML值已经插入。这是正确的方法,如果是这样,你可以给我code几行作为一个例子,以确保我做的对吗?谢谢!

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!

另外,我从来没有机会之前,要做到这一点,什么是阅读ASP.NET C#中的文件,该文件位于服务器上的最佳方式?

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?

更新:谢谢所有的答案!我已经找到了解决我的问题,但提供的所有三个答案都是接近这一挑战的好方法。正如您可以猜到,这是一个艰难的选择,谁给接受的答案,但我要去给它<一个href=\"http://stackoverflow.com/questions/1409257/asp-net-dynamicly-generating-html-how-to/1409276#1409276\">this回答,由敬畏,因为他清楚地把大量的精力投入到它,这是一个很优雅的解决方案,他回答这两个我的问题。谢谢大家的精彩回答!

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!

推荐答案

创建一个div是在服务器code访问:

Create a div that is accessible in server code:

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

在Page_Load中:

In Page_Load:

xmlGeneratedContent.InnerHtml = parcedHtmlFromXml;

编辑:

至于回答了最后一个问题:如何读取服务器上的文件...

如果该文件所在的网站下,你可以使用使用Server.Mappath 从相对URL获得物理磁盘位置:


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:

阅读全部一次:

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

阅读一下子到含有行字符串数组:

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

读取一行一次:

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();

这篇关于ASP.NET:动态地生成HTML,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆