将c#HtmlTextWriter页面转换为简单的asp.net

时间:2017-09-05 07:03:09

标签: c# asp.net

我有一个旧的showxml网页,它是使用HtmlTextWriter编写的。我没有足够的经验使用HtmlTextWriter方法,我有一些问题来控制它。我需要xsl,xml,js和它们的ID。因为页面元素使用其ID控制彼此。所以我想把它转换成简单的asp.net页面。这样做的方法是什么?以下是我的代码:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="C#" runat="server">
protected override void Render(HtmlTextWriter writer)
{
    Response.Buffer = true;
    Response.ContentEncoding = Encoding.UTF8;

    String number_appl = "20170001";
    String objIntel = "marks";
    String username;
    username = User.Identity.Name;

    writer.WriteFullBeginTag("html");
    writer.WriteLine();

    writer.WriteBeginTag("meta");
    writer.WriteAttribute("http-equiv", "Content-Type");
    writer.WriteAttribute("content", "text/html; charset=utf-8");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteLine();

    writer.WriteBeginTag("xml");
    writer.WriteAttribute("id", "template");
    writer.WriteAttribute("src", "/edit/source.aspx?objIntel=" + objIntel + "&filename=template.xml&un=");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("xml");
    writer.WriteLine();

    writer.WriteBeginTag("xml");
    writer.WriteAttribute("id", "data");
    writer.WriteAttribute("src", "/edit/source.aspx?objIntel=DataBase\\" + objIntel + "&filename=\\xmls\\" + number_appl + ".xml&un=" + username);
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("xml");
    writer.WriteLine();

    writer.WriteBeginTag("xml");
    writer.WriteAttribute("id", "style");
    writer.WriteAttribute("src", "/edit/source.aspx?objIntel=" + objIntel + "&filename=b7b.xsl&un=" + username);
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("xml");
    writer.WriteLine();

    writer.WriteBeginTag("xml");
    writer.WriteAttribute("id", "xmlerror");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteBeginTag("XMLDATA");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("XMLDATA");
    writer.WriteEndTag("xml");
    writer.WriteLine();

    writer.WriteBeginTag("LINK");
    writer.WriteAttribute("REL", "STYLESHEET");
    writer.WriteAttribute("HREF", "/edit/styles.css");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("LINK");
    writer.WriteLine();

    writer.WriteBeginTag("script");
    writer.WriteAttribute("language", "JScript");
    writer.WriteAttribute("src", "/edit/source.aspx?objIntel=" + objIntel + "&filename=showxml.js");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("script");
    writer.WriteLine();
    writer.WriteBeginTag("TITLE");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write(number_appl);
    writer.WriteEndTag("TITLE");
    writer.WriteLine();

    writer.WriteBeginTag("body");
    writer.WriteAttribute("alink", "#006666");
    writer.WriteAttribute("Link", "#006666");
    writer.WriteAttribute("Vlink", "#006666");
    writer.WriteAttribute("bgcolor", "#f0fff0");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteLine();

    writer.WriteBeginTag("DIV");
    writer.WriteAttribute("id", "showResult");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("DIV");
    writer.WriteLine();

    writer.WriteBeginTag("FORM");
    writer.WriteAttribute("ENCTYPE=", "multipart/form-data");
    writer.WriteAttribute("action", "savexml.aspx?un=" + username);
    writer.WriteAttribute("method", "POST");

    writer.WriteAttribute("name", "FORM1");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteBeginTag("INPUT");
    writer.WriteAttribute("type", "submit");
    writer.WriteAttribute("name", "Enter");
    writer.WriteAttribute("value", "Зафиксировать изменения");
    writer.WriteAttribute("onClick", "achSendDataToServer()");
    writer.WriteEndTag("INPUT");
    writer.WriteBeginTag("INPUT");
    writer.WriteAttribute("type", "hidden");
    writer.WriteAttribute("name", "blob");
    writer.WriteAttribute("value", "blob_xml");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("INPUT");
    writer.WriteBeginTag("INPUT");
    writer.WriteAttribute("type", "hidden");
    writer.WriteAttribute("name", "appl_number");
    writer.WriteAttribute("value", number_appl);
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("INPUT");
    writer.WriteBeginTag("INPUT");
    writer.WriteAttribute("type", "hidden");
    writer.WriteAttribute("name", "vid");
    writer.WriteAttribute("value", objIntel);
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("INPUT");
    writer.WriteBeginTag("INPUT");
    writer.WriteAttribute("type", "hidden");
    writer.WriteAttribute("name", "un");
    writer.WriteAttribute("value", username);
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("INPUT");
    writer.WriteEndTag("FORM");
    writer.WriteLine();

    writer.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?> <user name=\"u_name\"> </user>");


    writer.WriteEndTag("body");
    writer.WriteLine();
    writer.WriteEndTag("html");

}

</script>

1 个答案:

答案 0 :(得分:2)

翻译并不难做到,例如:

writer.WriteBeginTag("DIV");
writer.WriteAttribute("id", "showResult");
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEndTag("DIV");
writer.WriteLine();

结果

<div id="showResult"></div>

因为:

WriteBeginTag("DIV");写了<div

然后我们得到writer.WriteAttribute("id", "showResult");

WriteAttribute添加一个带有值的属性,在这种情况下,属性名称为&#34; id&#34;,值为&#34; showResult&#34;

writer.Write(HtmlTextWriter.TagRightChar);然后在我们开放的>标记的末尾写下<div,然后我们有<div id="showResult">

然后剩下的就是关闭标签,这是完成的 writer.WriteEndTag("DIV");。 (这会添加</div>

记住这个逻辑,你应该毫不费力地将这个代码翻译成一个体面的aspx页面。