can you use XmlTextWriter to create HTML?

J

John Salerno

I thought I might use the XML functions in C# to help me do some
repetitive typing in an XHTML file, but I'm stuck. Here's what I have
before I just stopped:

void WriteXMLFile()
{
string path = @"C:\elements.xml";

XmlTextWriter xml = new XmlTextWriter(path,
System.Text.Encoding.UTF8);
xml.Formatting = Formatting.Indented;

xml.WriteStartElement("div");
xml.WriteAttributeString("id", "parameters");

xml.WriteElementString
xml.WriteStartElement("p");
xml.WriteAttributeString("class", "i1");
}

The reason I stopped is because I somehow need to be able to create an
element that has both attributes and content, but none of the functions
I've read about so far (in C#) can do this. Am I missing them, or is it
just that XML documents don't have elements that have attributes and
content? Is it just one or the other? If so, is there some other way I
can write a simple program to automate some of my HTML?

Thanks.
 
G

Guest

I haven't tried this, but couldn't you pass your XmlTextWriter to the
constructor of a new HtmlTextWriter? Then the HTML generated by the
HtmlTextWriter would be merged at that point in the same TextWriter as the
XmlTextWriter.

HTH
 
J

John Salerno

Dale said:
I haven't tried this, but couldn't you pass your XmlTextWriter to the
constructor of a new HtmlTextWriter? Then the HTML generated by the
HtmlTextWriter would be merged at that point in the same TextWriter as the
XmlTextWriter.

HTH

wow, i didn't know there was an HtmlTextWriter...maybe that's what I
need after all. Thanks!
 
J

John Salerno

Dale said:
I haven't tried this, but couldn't you pass your XmlTextWriter to the
constructor of a new HtmlTextWriter? Then the HTML generated by the
HtmlTextWriter would be merged at that point in the same TextWriter as the
XmlTextWriter.

HTH

Hmm, I saw that there is also an XhtmlTextWriter class, which sounds
like what I want, but they both say something like they write to a
specific source or device. Is there a way for them to just write to a
text file?
 
G

Guest

You created the TextWriter to the text file when you created your
XmlTextWriter. All you have to do now is pass your XmlTextWriter as the
single parameter to the constructor of your HtmlTextWriter, continuing from
your earlier code:

HtmlTextWriter html = new HtmlTextWriter(xml);

As you probably noticed in the documentation, TextWriter is an abstract
class. You have to pass a subclass to the constructor of your
HtmlTextWriter. A common place to get such a TextWriter is
Page.CreateHtmlTextWriter or overriding the Render method in a WebControl.

If you want to start from scratch and skip the XML or Page step, you can use
a StreamWriter as the TextWriter to pass the HtmlTextWriter constructor
parameter and then you can write the HTML to a file on the file system,
assuming the process has the appropriate permissions.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top