Gerenate XML document

  • Thread starter Thread starter Diego F.
  • Start date Start date
D

Diego F.

Hi all. I need to create a log file for my web application and I was
thinking about create it in xml format, so I could use it later in my
application easily.

What is the fastest way to create xml documents? Should I use any particular
object or simply write the tags <.../> in the text file?
 
Diego said:
Hi all. I need to create a log file for my web application and I was
thinking about create it in xml format, so I could use it later in my
application easily.

What is the fastest way to create xml documents? Should I use any
particular object or simply write the tags <.../> in the text file?

I'm not sure XML is the best format for a logfile (a logfile typically
grows by adding "messages" at the end, and XML want a root-element
close-tag at the end), but to generate an XMl document (C#):

using System.IO;
using System.Text;
using System.Xml;

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xw = new XmlTextWriter(sw);

... now you can use commands like

xw.WriteStartElement("mytag");
xw.WriteElementString("othertag", "value");
xw.WriteEndElement();
(see documentation for XmlTextWriter)

... and get the final result with

sb.ToString();



Hans Kesting
 

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

Back
Top