472,974 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,974 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 5818
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.