This prior post demonstrated a very simple technique for writing to an XML file that has a set of elements. This post extends that example showing how to write an XML file that has both elements and attributes. Here is the XML file created by this example:<?xml version="1.0" encoding="utf-8"?>
<Employee>
<LastName>Baggins</LastName>
<FirstName>Bilbo</FirstName>
<PhoneNumber PhoneType="Work">(925)555-1234</PhoneNumber>
</Employee>This XML file has an Employee root node and LastName, FirstName, and PhoneNumber elements. The PhoneNumber element has a PhoneType attribute. There are several ways to write this file in C# or VB as shown below.In C# (adding individual nodes):var doc = new XDocument();var emp = new XElement("Employee");emp.Add(new XElement("LastName", textBox1.Text));
emp.Add(new XElement("FirstName", textBox2.Text));var phone = new XElement("PhoneNumber", textBox3.Text);
phone.Add(new XAttribute("PhoneType", comboBox1.Text));
emp.Add(phone); doc.Add(emp);doc.Save("Employee.xml");This code creates an XML document and adds an Employee root node. It then creates the three elements.Since the Add method does not return a value, the code needs to create the phone element separately. This provides a reference to that element that can be used to add the attribute. The Employee element is then added to the XML document and saved.In C# (using functional construction):var emp = new XElement("Employee",
new XElement("LastName", textBox1.Text),
new XElement("FirstName", textBox2.Text),
new XElement("PhoneNumber",
new XAttribute("PhoneType", comboBox1.Text),
textBox3.Text));emp.Save("Employee.xml"); Functional construction allows you to create an XML string in a single statement leveraging the XElement and XAttribute constructors. See this msdn link for more information on functional construction.This example also demonstrates how you can build an XML file without explicitly creating an XDocument object as was done in the prior example. In C# (using the Parse method):var emp = XElement.Parse(@"<Employee>
<LastName>" + textBox1.Text + @"</LastName>
<FirstName>" + textBox2.Text + @"</FirstName>
<PhoneNumber PhoneType='" + comboBox1.Text + "'>" + textBox3.Text + @"</PhoneNumber>
</Employee>");emp.Save("Employee.xml"); Read more: Deborah's Developer MindScape
<Employee>
<LastName>Baggins</LastName>
<FirstName>Bilbo</FirstName>
<PhoneNumber PhoneType="Work">(925)555-1234</PhoneNumber>
</Employee>This XML file has an Employee root node and LastName, FirstName, and PhoneNumber elements. The PhoneNumber element has a PhoneType attribute. There are several ways to write this file in C# or VB as shown below.In C# (adding individual nodes):var doc = new XDocument();var emp = new XElement("Employee");emp.Add(new XElement("LastName", textBox1.Text));
emp.Add(new XElement("FirstName", textBox2.Text));var phone = new XElement("PhoneNumber", textBox3.Text);
phone.Add(new XAttribute("PhoneType", comboBox1.Text));
emp.Add(phone); doc.Add(emp);doc.Save("Employee.xml");This code creates an XML document and adds an Employee root node. It then creates the three elements.Since the Add method does not return a value, the code needs to create the phone element separately. This provides a reference to that element that can be used to add the attribute. The Employee element is then added to the XML document and saved.In C# (using functional construction):var emp = new XElement("Employee",
new XElement("LastName", textBox1.Text),
new XElement("FirstName", textBox2.Text),
new XElement("PhoneNumber",
new XAttribute("PhoneType", comboBox1.Text),
textBox3.Text));emp.Save("Employee.xml"); Functional construction allows you to create an XML string in a single statement leveraging the XElement and XAttribute constructors. See this msdn link for more information on functional construction.This example also demonstrates how you can build an XML file without explicitly creating an XDocument object as was done in the prior example. In C# (using the Parse method):var emp = XElement.Parse(@"<Employee>
<LastName>" + textBox1.Text + @"</LastName>
<FirstName>" + textBox2.Text + @"</FirstName>
<PhoneNumber PhoneType='" + comboBox1.Text + "'>" + textBox3.Text + @"</PhoneNumber>
</Employee>");emp.Save("Employee.xml"); Read more: Deborah's Developer MindScape
0 comments:
Post a Comment