I'm writing an application in C#. I already have not empty .xml file but I want to add new values in it, without deleting some old.
I have tried these codes:
First:
FileStream docNewUser = new FileStream(@"C:\\MyApp\\MySubDir\\Data\\" + pr + ".xml", FileMode.Open);
XmlTextWriter xmlNewUser = new XmlTextWriter(docNewUser, null);
xmlNewUser.WriteStartDocument();
xmlNewUser.WriteStartElement("RootEl");//root
xmlNewUser.WriteStartElement("Zapis");
xmlNewUser.WriteStartElement("Name");
xmlNewUser.WriteString(txtEnterName.Text);
xmlNewUser.WriteEndElement();
xmlNewUser.WriteEndElement();//end of zapis
this.Close();
Second:
FileStream docNewUser = new FileStream(@"C:\\MyApp\\MySubDir\\Data\\" + pr + ".xml", FileMode.Open);
XmlTextWriter xmlNewUser = new XmlTextWriter(docNewUser, null);
xmlNewUser.WriteStartDocument();
xmlNewUser.WriteStartElement("RootEl");//root-ot
xmlNewUser.WriteStartElement("Zapis");
xmlNewUser.WriteStartElement("Name");
xmlNewUser.WriteString(txtEnterName.Text);
xmlNewUser.WriteEndElement();
xmlNewUser.WriteEndElement();//end of zapis
xmlNewUser.WriteElementString("Ime", null, txtEnterName.Text);
this.Close();
Third:
FileStream docNewUser = new FileStream(@"C:\\MyApp\\MySubDir\\Data\\" + pr + ".xml", FileMode.Open);
XmlTextWriter xmlNewUser = new XmlTextWriter(docNewUser, null);
xmlNewUser.WriteStartDocument();
xmlNewUser.WriteStartElement("Zapis");
xmlNewUser.WriteStartElement("Name");
xmlNewUser.WriteString(txtEnterName.Text);
xmlNewUser.WriteEndElement();
xmlNewUser.WriteEndElement();//end of zapis
xmlNewUser.WriteElementString("Ime", null, txtEnterName.Text);
this.Close();
I think the problem is that the stream doesn’t know where to put the new vali.
One more information – the root element is already entered.