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 开发者_StackOverflow社区large documents)
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.
For anyone else interested in this question, and if I understand the question correctly, and you want to rename an element node, then I see that it's very easily doable from XPathNavigator using ReplaceSelf. I'm using .Net framework version 4.0, but this looks like it's been around for a while.
(quick C# example)
XmlDocument reportServerDocument = new XmlDocument();
reportServerDocument.Load("C:\Path\to\ReportServer\rsreportserver.config");
XPathNavigator reportServerDocumentNavigator =
reportServerDocument.CreateNavigator();
XPathNavigator authenticationTypesNode =
reportServerDocumentNavigator.SelectSingleNode(
"//Authentication/AuthenticationTypes/RSWindowsNegotiate");
authenticationTypesNode.ReplaceSelf("<Custom/>");
reportServerDocument.Save("C:\Path\to\ReportServer\rsreportserver.config");
log.Info("Updated the AuthenticationTypes: " +
authenticationTypesNode.OuterXml);
精彩评论