XPathDocument.XPathNavigator throws NotSupportedException for AppendChild() and AppendchildElement() methods. Generating XPathNavigator from XmlDocument behaves as expected:
string contoso = @"
<bookstore> <book genre=""autobiography"" publicationdate=""1981-03-22"" ISBN=""1-861003-11-0""> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre=""novel"" publicationdate=""1967-11-17"" ISBN=""0-201-63361-2""> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre=""philosophy"" publicationdate=""1991-02-15"" ISBN=""1-861001-57-6""> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore> " ; XmlDocument document = new XmlDocument(); document.LoadXml( contoso ); XPathNavigator navigator = document.CreateNavigator();
navigator.MoveToChild( "bookstore", "" );
navigator.MoveToChild( "book", "" );
navigator.AppendChildElement( "", "pages", "", "100" );
Console.WriteLine( navigator.OuterXml );
However, the following (with XPathDocument) does not work:
XPathDocument document =
new XPathDocument( new StringReader( contoso ) );
XPathNavigator navigator = document.CreateNavigator();
navigator.MoveToChild( "bookstore", "" );
navigator.MoveToChild( "book", "" );
navigator.AppendChildElement( "", "pages", "", "100" );
//NotSupportedException thrown here!
Console.WriteLine( navigator.OuterXml );
To be generous, one can argue that this behavior is “by design” because the XPathdocument is designed to be read only.
The examples here are derived from the Microsoft documentation:
http://msdn.microsoft.com/en-us/library/
system.xml.xpath.xpathnavigator.appendchildelement.aspx