How to create inmemory HTML/XML document in .NET?
I wou开发者_运维问答ld like to write application which will iterate trough certain test cases and generated output in HTML file.
For this i would like to have something using which i can keep appending the HTML nodes to the output for each test case. Is there nice way in .NET for doing so ? How ?
Any pointers or suggestions will be helpful.
You can use an XmlWriter
that uses a MemoryStream
as the base stream.
XmlWriter inMemHTML = XmlWriter.Create(new MemoryStream());
If you want to do this the XML way, you can use the XmlDocument class (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) to help generate XML. Obviously you will be generating XHTML as your output.
Another alternative would be to generate XML in some other dialect (one you make up to suit your business needs for example), and then transform the generated XML into XHTML using XSLT.
It's also worth asking if the XML step is needed at all. You could for example use the StringBuilder class to generate HTML as a string.
Hope this helps.
David
You can use XElement, XDocument and others, too.
I do not know how this would handle nodes that must not be empty, i.e.
<br />
<div></div>
just in case you want to generate XHTML.
精彩评论