Search code examples
c#.netxmlxpathnavigator

Rename an element using XPathNavigator


I have an XPathNavigator object pointing to an XML element. I want to rename the element to another name (and also rename the associated end element). Can this be done using the XPathNavigator?

(I have a work-around, which is to Delete the element and re-insert it under a different name, but this may cause a performance issue, because I am handling very large documents)


Solution

  • It depends on what your underlying XML document representation is. If you are using XDocument you can do:

    (XElement)(navigator.UnderlyingObject).Name = ...
    

    I don't think it is possible with XmlDocument (except as you suggest), or XPathDocument.