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.

Here is some code I used for an exercise program that recorded the user's workout (exercises, weights and reps) and stored it into an xml file under the "Weights" element.
Essentially it locates the element I wish to add the data ito ("Weights"), creates the new nodes and adds the attribute values to them and then adds the new nodes to the existing element.

There is a loop where I'm running through a populated dataTable extracting the data. ignore that and concentrate on the attribute and node creation/adding aspects and you should be able to follow it for your code.

XmlDocument doc = new XmlDocument();
                doc.Load("Users/" + profile + ".xml");

                XmlNodeList xmlNodeList1 = doc.SelectNodes("//Weights");
                XmlNode newNode;
                XmlNode setNode;

                // create elements, attributes and specify values
                XmlAttribute dateAtt = doc.CreateAttribute("date");
                dateAtt.Value = date;
                XmlAttribute weightAtt = doc.CreateAttribute("weight");
                XmlAttribute repsAtt = doc.CreateAttribute("rep");

                foreach (XmlNode existingNode in xmlNodeList1)
                {
                    newNode = doc.CreateElement("Session");
                    // child nodes of the newNode
                    
                    
                    // for loop cycles through ds dataset adding set elements as needed
                    // nameNode takes on datatable name value
                    // set element is added for each row in datatables
                    Int32 tables = ds.Tables.Count;
                    for (int i = 0; i < tables; i++)
                    {
                        XmlNode exerNode;
                        exerNode = doc.CreateElement("exercise");
                        XmlNode nameNode;
                        nameNode = doc.CreateElement("name");
                        nameNode.InnerText = ds.Tables[i].TableName.ToString();
                        exerNode.AppendChild(nameNode);
                        Int32 rows = ds.Tables[i].Rows.Count;
                        for (int x = 0; x < rows; x++)
                        {
                            setNode = doc.CreateElement("set");
                            weightAtt = doc.CreateAttribute("weight");
                            repsAtt = doc.CreateAttribute("reps");
                            weightAtt.Value = ds.Tables[i].Rows[x].ItemArray[0].ToString();
                            repsAtt.Value = ds.Tables[i].Rows[x].ItemArray[1].ToString();
                            setNode.Attributes.Append(weightAtt);
                            setNode.Attributes.Append(repsAtt);
                            exerNode.AppendChild(setNode);
                            newNode.AppendChild(exerNode);
                        }
                    }

                    newNode.Attributes.Append(dateAtt);                    
                    
                    existingNode.AppendChild(newNode);
                }
                doc.Save("Users/" + profile + ".xml");

Hope that helps,

This article has been dead for over six months. Start a new discussion instead.