Append new node to XPathNavigator : XPath « XML « C# / CSharp Tutorial






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");
        }
    }








30.21.XPath
30.21.1.XPath Select Nodes
30.21.2.Append child from XPathNavigator
30.21.3.Append new node to XPathNavigator
30.21.4.Using XPath to filter data from Database
30.21.5.Using XPathNavigator to loop through code
30.21.6.Using XPathNodeIterator
30.21.7.XPath Navigator
30.21.8.XPath Node Iterator
30.21.9.XPath Selecting: /Customers/Customer[@FirstName='D']
30.21.10.XPath XSLT transformation
30.21.11.XPath selecting: descendant::Customer[@FirstName='D']
30.21.12.XPath selecting: descendant::Customer[EmailAddress='k@a.com']
30.21.13.XPath selecting: descendant::Customer[attribute::FirstName='D']
30.21.14.XPath selecting: descendant::Customer[contains(EmailAddress, 'a.com')]
30.21.15.XPath selecting: descendant::Customer[starts-with(@LastName, 'G') and contains(EmailAddress, 'a.com')]
30.21.16.XPath selecting: descendant::EmailAddress[text()='k@a.com']
30.21.17.XPathNavigator and TreeView
30.21.18.Xpath Demo: //attribute[@name='capacity'] | //attribute[@name='weight']
30.21.19.Xpath Demo: /items/item
30.21.20.Xpath Demo: /items/item/attribute[@value > 10 and @name='weight']
30.21.21.Xpath Demo: /items/item[2]
30.21.22.Create XPathNavigator
30.21.23.Move to first child in XPathNavigator
30.21.24.Compile XPath to create XPathExpression
30.21.25.Read sub tree from XPathNavigator
30.21.26.Select All Author Nodes
30.21.27.Select All Books
30.21.28.Select All Child Nodes
30.21.29.Select XPath from XPathNavigator
30.21.30.Set new value through XPathNavigator
30.21.31.Show innter xml and outer xml from XPathNavigator
30.21.32.Sorting XPathExpression with XmlSortOrder.Ascending, XmlCaseOrder.None
30.21.33.Using XPathExpression to sort by first name
30.21.34.Select node with asterisk
30.21.35.Use the XPath return type to determine how to process the XPath expression