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

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

问题描述

在过去一年中,我一直在做ASP.NET(开和关),但我从来没有遇到过这样的挑战:我正在建立一个很简单的网站,主要是基于HTML和Javascript。但是,在一个页面上,我需要从服务器上读取一个XML文件,解析它,从XML文件中包含的值创建HTML,并将其作为响应输出。我将使用ASP.NET与C#为此。我了解如何解析XML并在C#中生成HTML代码,但是如何将HTML代码写入到页面的响应中?生成的动态HTML只在页面的一个大div中,页面的其余部分是静态的。这样做最好的方法是什么?由于我以前从未做过任何这样的事情,所以我猜测一种方法是清除页面的整个HTML源代码,并在Page_Load事件中使用Response.Write()写入整个HTML该页面已经插入了XML值。这是否是正确的方法,如果是这样,你可以给我几行代码作为例子,以确保我做的正确吗?谢谢!

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?

更新:感谢您的所有答案!我已经找到了解决问题的方法,但是提供的所有三个答案都是处理这一挑战的好方法。你可以猜到,这是一个艰难的选择谁给出了接受的答案,但我要给它,因为他清楚地付出了很多努力,这是一个非常优雅的解决方案,他回答了我的两个问题。感谢大家的精彩答案!

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:

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天全站免登陆