473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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 5843
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Magnus | last post by:
can anyone help me on how to create and manipulate a xmttextwriter without having to craete a physical file. I have an application that should return data in xml. But I do not want to create a...
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
0
by: Guoqi Zheng | last post by:
Dear sir, I am trying to use XmlTextWriter to create a xml file. It works ok, however when I reexecute the aspx page again, it always give an error of "The process cannot access the file...
1
by: Terrence Chan | last post by:
Does anyone know the size limit of the StringWriter and XmlTextWriter. Dim buffer As New System.IO.StringWriter Dim writer As New System.Xml.XmlTextWriter(buffer) Please point me to some...
4
by: quest | last post by:
Is there anyway I can generate the xml in the following format using XmlTextWriter ? Intended output: <?xml version="1.0" ?> I tried: XmlTextWriter xmlWriter = new...
2
by: Mr Flibble | last post by:
Hey Folks. If I want to create a simple XMLDocument that contains a few lines of XML which class should I use? I'm a bit confused of the differences and which class to use and when. Ta.
3
by: GaryDean | last post by:
I'm using an XmlTextWriter and it's various methods such as WriteElementString, WriteStartElement, WriteEndElement, etc to create an xml document. When I instantiate the XmlTextWriter to a file......
6
by: clintonG | last post by:
Can anybody make sense of this crazy and inconsistent results? // IE7 Feed Reading View disabled displays this raw XML <?xml version="1.0" encoding="utf-8" ?> <!-- AT&T HTML entities & XML...
1
by: mrdanny2007 | last post by:
Is there anyway to get the XML string representation from the XmlTextWriter object? I created a function to create an XML using this object, but I need to send back an XML as the return value of...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.