Dodawanie elementow do Xml

0

Witam. Tworzę dokument Xml w poniższy sposób.

XmlElement root = doc.CreateElement("Pracownicy");
            doc.AppendChild(root);

            XmlElement studenci = doc.CreateElement("Pracownik");
            root.AppendChild(studenci);

            XmlElement name = doc.CreateElement("Imie");
            name.AppendChild(doc.CreateTextNode("Jan"));
            studenci.AppendChild(name);
            XmlElement surname = doc.CreateElement("Nazwisko");
            surname.AppendChild(doc.CreateTextNode("Kowalski"));
            studenci.AppendChild(surname);
            XmlElement group = doc.CreateElement("Wiek");
            group.AppendChild(doc.CreateTextNode("36"));
            studenci.AppendChild(group);


            XmlTextWriter save = new XmlTextWriter("pracownicy.xml", null);
            doc.Save(save);

Mój problem polega na tym, że nie wiem jak napisać metodę dodającą kolejne drzewa do tego dokumentu (mam na myśli kolejne osoby), niestety zawsze dokument zostaje nadpisany. Mógłby mi ktoś przedstawić jakąś prostą formułę na dodawanie tego węzła ? Będę wdzięczny. Pozdrawiam

0

debuguj - sprawdź aktualną zawartość

0
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"C:\test\xml1.xml");
            XmlNodeList xmlNodeList1 = xmlDoc.SelectNodes("//item");
            XmlNode newNode;
            foreach (XmlNode existingNode in xmlNodeList1 )
            {
                newNode  = xmlDoc.CreateElement("newItem");

existingNode.AppendChild(newNode );
                //existingNode.ParentNode.InsertAfter(newNode,
existingNode); 
            }
            xmlDoc.Save(@"C:\test\xml2.xml");

http://www.developer.com/net/csharp/article.php/3489611

1 użytkowników online, w tym zalogowanych: 0, gości: 1