1

我正在使用 HTMLTEXTWRITER 动态创建一个 HTML 文件,我可以在浏览器中查看它,但是当我尝试在 iframe 中查看这个 html 时,什么都没有显示!动态生成的 html 有什么问题?iframe 也不在 Firefox 中显示一些 HTML,这是我创建 html 的方式:

static string GetDivElements()
    {
        // Initialize StringWriter instance.
        StringWriter stringWriter = new StringWriter();

        // Put HtmlTextWriter in using block because it needs to call Dispose.
        using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
        {
            // Loop over some strings.
            foreach (var word in _words)
            {
                // Some strings for the attributes.
                string classValue = "ClassName";
                string urlValue = "http://www.dotnetperls.com/";
                string imageValue = "image.jpg";

                // The important part:
                writer.AddAttribute(HtmlTextWriterAttribute.Class, classValue);
                writer.RenderBeginTag(HtmlTextWriterTag.Div); // Begin #1
...my html
                writer.RenderBeginTag(HtmlTextWriterTag.Img); // Begin #3
                writer.RenderEndTag(); // End #3

                writer.Write(word);

                writer.RenderEndTag(); // End #2
                writer.RenderEndTag(); // End #1
            }
        }
        // Return the result.

        string pre = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
        pre += "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>";
        pre += "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><title></title></head><body>";

        return stringWriter.ToString();// +"</body></html>";
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        using (StreamWriter outfile =
        new StreamWriter(@"d:\myFile.htm"))
        {
            outfile.Write(GetDivElements());
        }
    }
4

0 回答 0