I'm building an Parts app in order to learn C# and WPF. I trying having trouble adding new parts using XmlWriter. I can create the xml file, but can not figure how to add additional parts. Should I be using something else like XmlDocument? Here is my code behind:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create("f:\\MyParts.xml", settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("MyParts");
writer.WriteStartElement("parts");
writer.WriteStartElement("item");
writer.WriteString(txtbxitem.Text);
writer.WriteEndElement();
writer.WriteStartElement("color");
writer.WriteString(txtbxcolor.Text);
writer.WriteEndElement();
writer.WriteStartElement("size");
writer.WriteString(txtbxsize.Text);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}
This code creates the xml file and a node correctly, but how do I add additional parts? Here is what I am trying to create:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<MyParts>
<parts>
<item>Part1</item>
<color>Red</color>
<size>SM</size>
</parts>
<parts>
<item>Part2</item>
<color>Blue</color>
<size>XXL</size>
</parts>
</MyParts>