In this article, i'll show you how to create and update an XML file in C#. Hope this help!
Create an XML file
Let's start by creating a new XML file. To do this, we'll use XmlTextWriter. Open your Main function and copy the code below:
static void Main(string[] args) {
// create a new file
string path = "E:\\Example.xml";
XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.UTF8);
// set output's view format
writer.Formatting = Formatting.Indented;
// write XML declaration
writer.WriteStartDocument(true);
// compose XML content
writer.WriteStartElement("inventory");
writer.WriteAttributeString("category", "vehicle");
writer.WriteStartElement("item");
writer.WriteAttributeString("id","p1");
writer.WriteString("Car");
writer.WriteEndElement();
writer.WriteStartElement("item");
writer.WriteAttributeString("id", "p2");
writer.WriteString("Bicycle");
writer.WriteEndElement();
writer.WriteEndElement();
// close session
writer.Close();
}