本文介绍了C#编写的html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码。有没有一种方法,使之更容易编写和维护?
使用C#.NET 3.5
I have code like this. Is there a way to make it easier to write and maintain?Using C# .NET 3.5
string header(string title)
{
StringWriter s = new StringWriter();
s.WriteLine("{0}","<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
s.WriteLine("{0}", "<html>");
s.WriteLine("<title>{0}</title>", title);
s.WriteLine("{0}","<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">");
s.WriteLine("{0}", "</head>");
s.WriteLine("{0}", "<body>");
s.WriteLine("{0}", "");
}
-edit-我不知道它,然后,但我可以写
-edit- i didnt know it then but i could write
s.WriteLine("{0}", @"blah blah
many
new
lines
blah UHY#$&_#$_*@Y KSDSD<>\t\t\t\t\t\tt\t\t\\\t\t\t\t\\\h\th'\h't\th
hi
done");
和它可以工作,但需要与
and it will work but need to replace all " with ""
推荐答案
你可能会更好过使用的或一个的比一个普通的StringWriter
他们会采取逃避你,以及确保是良好的形成文件的照顾。
You're probably better off using an HtmlTextWriter
or an XMLWriter
than a plain StringWriter
. They will take care of escaping for you, as well as making sure the document is well-formed.
的显示了使用的HtmlTextWriter
类的基本知识。
This page shows the basics of using the HtmlTextWriter
class.
这篇关于C#编写的html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!