Use objects to make an XML document in C#
There are several ways to build an XML document hierarchy. The example Build a formatted XML document in memory by using an XmlTextWriter in C#shows one way to build an XML document as a formatted string that you can then display or write into a file.
Another approach is to use the XML Document Object Model (DOM) to build objects representing the data. This example demonstrates this approach. 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 makes a new XmlDocument. It creates a root element and adds it to the XmlDocument. Note that the XmlDocument can contain only one 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 in a text box.
private void btnGo_Click(object sender, EventArgs e)The MakeEmployee method 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 and sets their InnerText values to place text inside the elements.
{
// 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;
}
// Add an Employee node to the document.If you run the example, you'll see that the XML file is correct but has no formatting so it is 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.
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);
}


Comments