first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

XPathDocument.XPathNavigator Throws NotSupportedException for AppendChild() and AppendchildElement() Methods

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( &quot;bookstore&quot;, &quot;&quot; );
navigator.MoveToChild( &quot;book&quot;, &quot;&quot; );

navigator.AppendChildElement( &quot;&quot;, &quot;pages&quot;, &quot;&quot;, &quot;100&quot; );

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( &quot;bookstore&quot;, &quot;&quot; );
navigator.MoveToChild( &quot;book&quot;, &quot;&quot; );

navigator.AppendChildElement( &quot;&quot;, &quot;pages&quot;, &quot;&quot;, &quot;100&quot; );
//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
mod date: 2008-05-02T16:45:15.000Z