views:

229

answers:

1

I have the following code, whose last line results in a NotSupportedException on every execution, and I haven't found a way around it. This hypothetical analogous code finds a "book" with a specific title, with the goal of updating it to the new title. It does find the correct node, but fails to update it.

XPathDocument xpathDoc = new XPathDocument( fileName );
XPathNavigator nav = xpathDoc.CreateNavigator();
XPathNavigator node = nav.SelectSingleNode( @"//Book[Title='OldTitle']/Title" );

node.SetValue( "NewTitle" );

Any help would be greatly appreciated.

+3  A: 

XPathNavigator objects created by XPathDocument objects are read-only (see MSDN: Remarks)
It should be created with XMLDocument to be editable:

XMLDocument xmlDoc = new XMLDocument( fileName );
XPathNavigator nav = xmlDoc.CreateNavigator();
XPathNavigator node = nav.SelectSingleNode( @"//Book[Title='OldTitle']/Title" );

node.SetValue( "NewTitle" );
najmeddine
But how can I use an XPath expression to find a node from an XMLDocument?
Dov
Never mind, it's the same CreateNavigator() method. I didn't notice that before. Awesome, thanks!
Dov
I edited my question to show that.
najmeddine