Build a formatted XML document in memory by using an XmlTextWriter in C#

When the user clicks the Go button, the program 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 MakeEmployee 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 StreamReader's ReadToEnd method to read the stream's contents.

The following code shows the main part of the program that controls the process.


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();
}

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


// 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();
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.