[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Build a formatted XML document in memory in C#


This example uses an XmlTextWriter to build a formatted XML document in memory and displays it in a text box. When you click the Go button, the following code executes.

private void btnGo_Click(object sender, EventArgs e) { MemoryStream memory_stream = new MemoryStream(); XmlTextWriter xml_text_writer = new XmlTextWriter(memory_stream, System.Text.Encoding.UTF8); // Use indentation to make the result look nice. xml_text_writer.Formatting = Formatting.Indented; xml_text_writer.Indentation = 4; // Write the XML declaration. xml_text_writer.WriteStartDocument(true); // Start the Employees node. xml_text_writer.WriteStartElement("Employees"); // Write some Employee elements. MakeEmployee(xml_text_writer, "Albert", "Anders", 11111); MakeEmployee(xml_text_writer, "Betty", "Beach", 22222); MakeEmployee(xml_text_writer, "Chuck", "Cinder", 33333); // End the Employees node. xml_text_writer.WriteEndElement(); // End the document. xml_text_writer.WriteEndDocument(); xml_text_writer.Flush(); // Use a StreamReader to display the result. StreamReader stream_reader = new StreamReader(memory_stream); memory_stream.Seek(0, SeekOrigin.Begin); txtResult.Text = stream_reader.ReadToEnd(); txtResult.Select(0, 0); // Close the XmlTextWriter. xml_text_writer.Close(); }

This code makes a MemoryStream and an XmlTextWriter attached to it. The XmlTextWriter has methods such as WriteStartDocument, WriteStartElement, and WriteEndElement to create the pieces of the XML file.

The program sets the XmlTextWriter so it indents its output and then calls its WriteStartDocument method to start the XML document.

It calls the WriteStartElement method to create the <Employees> start tag. It then calls the MakeEmployee method several times to make Employee elements. It closes the Employees element by calling WriteEndElement to create the </Employees> end tag. It then closes the document by calling WriteEndDocument.

To display the result, the program makes a StreamReader attached to the MemoryStream. It moves to the beginning of the stream and uses the reader's ReadToEnd method to read the stream's contents.

The following code shows the MakeEmployee method.

// Add an Employee node to the document. private void MakeEmployee(XmlTextWriter xml_text_writer, String first_name, String last_name, int emp_id) { // Start the Employee element. xml_text_writer.WriteStartElement("Employee"); // Write the FirstName. xml_text_writer.WriteStartElement("FirstName"); xml_text_writer.WriteString(first_name); xml_text_writer.WriteEndElement(); // Write the LastName. xml_text_writer.WriteStartElement("LastName"); xml_text_writer.WriteString(last_name); xml_text_writer.WriteEndElement(); // Write the EmployeeId. xml_text_writer.WriteStartElement("EmployeeId"); xml_text_writer.WriteString(emp_id.ToString()); xml_text_writer.WriteEndElement(); // Close the Employee element. xml_text_writer.WriteEndElement(); }

The MakeEmployee method simply uses the XmlTextWriter's WriteStartElement, WriteString, and WriteEndElement methods to build an Employee element.

Using an XmlTextWriter to build an XML document isn't too hard, although it is long and you need to be sure to create end elements where they are needed.

Download the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.