471,319 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

can you use XmlTextWriter to create HTML?

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.
Jan 27 '06 #1
4 5710
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
--
Dale Preston
MCAD C#
MCSE, MCDBA
"John Salerno" wrote:
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.

Jan 27 '06 #2

Dale wrote:
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!
Jan 27 '06 #3
Dale wrote:
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?
Jan 27 '06 #4
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
--
Dale Preston
MCAD C#
MCSE, MCDBA
"John Salerno" wrote:
Dale wrote:
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?

Jan 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Magnus | last post: by
reply views Thread by Guoqi Zheng | last post: by
1 post views Thread by Terrence Chan | last post: by
4 posts views Thread by quest | last post: by
3 posts views Thread by GaryDean | last post: by
6 posts views Thread by clintonG | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.