I have a pretty complex composite application (.NET/WPF/Prism). It runs on some kind of embedded devices (with full fledge window 7), let's call them "kiosks". I need to al开发者_如何学Pythonlow remote control for the applicaton. i.e. it should have an embedded web-server which will provide some information via http/html for remote control (checking status, logs, etc).
So in some application's module I need to compose HTML to return it. This seems to be pretty tedious. It looks like:
using (Stream ostr = ...)
using (TextWriter tw = new StreamWriter(ostr))
{
tw.WriteLine("<html>");
tw.WriteLine("<head>");
tw.WriteLine("<title>Web Console</title>");
tw.WriteLine("<meta http-equiv=\"refresh\" content=\"2\" />");
}
I want to simplify HTML creation process. So I'm looking for some kind of .NET HTML DOM library. And can't find decent one. Only one I've found so far - HTML Agility Pack. It looks cool but I believe it targets a bit other use-case (load some HTML and search with xpath/linq). But I need just compose HTML, not parse, not query. I'd expect from a library API similar to System.Xml.Linq.
Something like this:
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.Head.AddScript(ScriptLanguages.JavaScript, "http://localhost/scripts/jquery.js");
htmlDoc.Body.Elements(
new HtmlDiv {
Style="align:center",
Content = new []{new TextBlock("Hi")} },
new HtmlDiv (
new HtmlLabel(text: "StartDate", nameFor: "dtStartDate"),
new HtmlTextBox(clintName: "dtStartDate", style: "width:100px")
)
);
Another posibility that comes to my mind is to use some kind of templating engine. But I have no idea which one will work seamessly with embedded web-server.
I'm open for you thoughts, thanks in advance.
p.s. If you wonder "Embedded web-server" is http://www.codeproject.com/KB/IP/CSharpEmbeddedHTTPServer.aspx
I don't know about you, but the HTML seems simpler to write read and understand. What benefit are you expecting by generating html in the way you suggested?
As to a template engine, you can easily create one yourself. Create an html page (one or more) with the theme you want. Add tags like %%title%% %%content%%, load the file do a regexp replacement, then serve the html. Wrap it up in a class and you have a very simple, flexible and easy to maintain solution.
精彩评论