views:

17

answers:

1

Below code gives this error message "Specified method is not supported". But here is sample which is same with mine.

        FileInfo file = new FileInfo("../../file.xml");
        XDocument xfile = XDocument.Load(file.FullName);
        XPathNavigator nav = xfile.CreateNavigator();

        nav.AppendChild("<pages>100</pages>");
+2  A: 

When you create an XPathNavigator from an XML source object, the navigator ultimately calls back into the original object to read its data and make its changes. The code sample you provided is not the same as the one you link to, because they are creating the XPathNavigator from an XmlDocument, which is read-write. You are creating one from an XDocument, which is read-only.

Each type of XML object returns its own version of an XPathNavigator, which is limited by the capabilities of the object it came from.

Rex M
Given error message doesn't make any sense for me.I hadn't realized that sample use XmlDocument.
Freshblood
@Freshblood it makes more sense once you understand each type of XML object returns its own version of an XPathNavigator, which is limited by the capabilities of the object it came from.
Rex M
@ Rex M Isn't it too stupidly that that method is not restricted by access If it is read only ?
Freshblood
@Freshblood It's a trade-off for abstracting the Navigator away from its origin. You can write code that reuses XPathNavigators regardless of what type of object they came from, which is awesome, but you have to be aware of the limitations of that. `Stream` works the same way - not all streams can be reset, for example, even though they all have a method called `Reset()`.
Rex M