You are on page 1of 1

string PATH = "E:\\xmldata.

xml";
XmlDocument doc = new XmlDocument();
//If there is no current file, then create a new one
if (!System.IO.File.Exists(PATH))
{
//Create neccessary nodes
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UT
F-8", "yes");
XmlComment comment = doc.CreateComment("This is an XML Generated
File");
XmlElement root = doc.CreateElement("Product");
XmlElement Product_ID = doc.CreateElement("Product_ID");
XmlElement Product_Name = doc.CreateElement("Product_Name");
XmlElement Product_Price = doc.CreateElement("Product_Price");
XmlElement Product_Quantity = doc.CreateElement("Product_Quantit
y");
XmlElement Product_Description = doc.CreateElement("Product_Desc
ription");
//Add the values for each nodes
Product_ID.Value = textBox1.Text;
Product_Name.Value = textBox2.Text;
Product_Price.Value = textBox3.Text;
Product_Quantity.Value = textBox4.Text;
Product_Description.Value = textBox5.Text;
//Construct the document
doc.AppendChild(declaration);
doc.AppendChild(comment);
doc.AppendChild(root);
root.AppendChild(Product_ID);
root.AppendChild(Product_Name);
root.AppendChild(Product_Price);
root.AppendChild(Product_Quantity);
root.AppendChild(Product_Description);
doc.Save(PATH);

You might also like