class Customer
{
private string firstName, lastName, emailAddress;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private List<Customer> CreateCustomer()
{
List<Customer> result = new List<Customer>{
new Customer{FirstName="Tom",LastName="James",EmailAddress="aaa@qq.com"},
new Customer{FirstName="Helen",LastName="Christina",EmailAddress="bbb@qq.com"},
new Customer{FirstName="Meimei",LastName="Han",EmailAddress="ccc@qq.com"}
};
return result;
}
public void CreateXML()
{
string path = Environment.CurrentDirectory + @"\data.xml";
List<Customer> customers = CreateCustomer();
XmlDocument customerXml = new XmlDocument();
XmlElement root = customerXml.createElement_x_x("Customers");
customerXml.AppendChild(root);
foreach (Customer c in customers)
{
XmlElement element = customerXml.createElement_x_x("Customer");
XmlElement FirstNameElement = customerXml.createElement_x_x("FirstName");
FirstNameElement.InnerText = c.FirstName;
element.AppendChild(FirstNameElement);
XmlElement LastNameElement = customerXml.createElement_x_x("LastName");
LastNameElement.InnerText = c.LastName;
element.AppendChild(LastNameElement);
XmlElement AddressElement = customerXml.createElement_x_x("Address");
AddressElement.InnerText = c.EmailAddress;
element.AppendChild(AddressElement);
root.AppendChild(element);
}
customerXml.Save(path);
}
private void btnCreateXML_Click(object sender, EventArgs e)
{
CreateXML();
}
}