Search code examples
xpathc#-3.0xpathnavigator

XpathNavigator raise error NotSupportedException


I want to update XDocument using the following code

private static bool ResetUpdateVersion()
{
    // this indicate either the verwsion is different or not
    // this will either call the update only or writting the defualt
    bool Result = false;
    //// check for version using xpath
    XPathNavigator navigator = document.CreateNavigator();
    //ShortcutList is the main element that contain all the other elements 
    XPathNavigator node = navigator.SelectSingleNode(@"/ShortcutList");
    XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
    if (node != null)
    {
        if (node.GetAttribute("Version", ns.DefaultNamespace) != Version)
        {
            node = navigator.SelectSingleNode(@"/ShortcutList/@Version");

            node.SetValue( Version);

            Result = true;
        }
        else
        {
            Result = false;
        }
    }

    return Result;
}

but it raise NotSupportedException on the line node.SetValue( Version); , I don't know why , any idea to solve that


Solution

  • An XPathNavigator over an XDocument or XElement is readonly, if you want to manipulate an XDocument or XElement then use the APIs exposed in System.Xml.Linq (like e.g. http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.setvalue.aspx).