Use objects to make an XML document in C#

example

This example shows how to use the XML DOM (document object model) to make an XML document. There are several ways to build an XML document. The example Build a formatted XML document in memory in C# shows one way to build an XML document as a formatted string that you can then display or write into a file.

This example uses a different approach. It uses the XML DOM to build objects representing the data. It’s relatively straightforward with only two real catches. First, an element must be created by the XmlDocument that will contain it. Second, you must add the element to a parent element (or the main XmlDocument for the root element) to actually make it part of the model.

When the user clicks the Go button, the program executes the following code.

private void btnGo_Click(object sender, EventArgs e)
{
    // Make the XML document.
    XmlDocument xml_document = new XmlDocument();

    // Make the root element.
    XmlElement employees_element =
        xml_document.CreateElement("Employees");
    xml_document.AppendChild(employees_element);

    // Make some Employee elements.
    MakeEmployee(employees_element, "Albert", "Anders", 11111);
    MakeEmployee(employees_element, "Betty", "Beach", 22222);
    MakeEmployee(employees_element, "Chuck", "Cinder", 33333);

    txtResult.Text = xml_document.OuterXml;
}

This code makes a new XmlDocument. It creates a root <Employees> element and adds it to the XmlDocument. Note that the XmlDocument can contain only one direct child element.

The program then calls the MakeEmployee method to make some Employee elements. When it finishes, the program displays the document’s outer XML (all of its XML code) in a text box.

The following code shows the MakeEmployee method.

// Add an Employee node to the document.
private void MakeEmployee(XmlElement parent,
    String first_name, String last_name, int emp_id)
{
    // Make the Employee element.
    XmlNode employee_element =
        parent.OwnerDocument.CreateElement("Employee");
    parent.AppendChild(employee_element);

    // Add the FirstName, LastName, and EmployeeId elements.
    XmlNode first_name_element =
        parent.OwnerDocument.CreateElement("FirstName");
    first_name_element.InnerText = first_name;
    employee_element.AppendChild(first_name_element);

    XmlNode last_name_element =
        parent.OwnerDocument.CreateElement("LastName");
    last_name_element.InnerText = last_name;
    employee_element.AppendChild(last_name_element);

    XmlNode employee_id_element =
        parent.OwnerDocument.CreateElement("EmployeeId");
    employee_id_element.InnerText = emp_id.ToString();
    employee_element.AppendChild(employee_id_element);
}

This code takes a parent element and some text as parameters. It uses the parent’s OwnerDocument property to get a reference to the XmlDocument containing the parent and uses its CreateElement method to make a new Employee element. It then creates FirstName, LastName, and EmployeeId elements. It sets their InnerText values to place text inside the elements.

When you use an XmlTextWriter to build an XML document, you must explicitly close open elements appropriately. When you use the DOM, objects represent pieces of the hierarchy. When you convert the DOM model’s objects into XML text, they automatically provide closing elements as necessary.

If you run the example, you’ll see that the XML file is correct but it has no formatting so it’s all run together on one long line. In the next example I’ll show how to format the text to make it easier to read.


Download Example   Follow me on Twitter   RSS feed




This entry was posted in XML and tagged , , , , , , , , , , . Bookmark the permalink.

One Response to Use objects to make an XML document in C#

  1. Pingback: Format an XML file so it looks nice in C# -

Leave a Reply

Your email address will not be published. Required fields are marked *