Mega Code Archive

 
Categories / C# Tutorial / XML
 

Append new node to XPathNavigator

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.XPath;     public class MainClass     {         public static void Main()         {             XmlDocument doc;             XPathNavigator editor2;             XmlWriter writer;             doc = new XmlDocument();             doc.Load("pubs.xml");             doc.CreateNavigator().Select("/pubs/titles[authors/@au_lname='Green']");             foreach(XPathNavigator editor in doc.CreateNavigator().Select("/pubs/titles[authors/@au_lname='Green']"))             {                 editor2 = editor.SelectSingleNode("authors[@au_lname!='Green']");                 if (editor2!=null) editor2.DeleteSelf();                 writer = editor.AppendChild();                 writer.WriteStartElement("authors");                 writer.WriteAttributeString("au_lname", "A");                 writer.WriteAttributeString("au_fname", "B");                 writer.Close();             }             doc.Save("output.xml");         }     }