【问题标题】:XPathNavigator.SetValue Throws NotSupportedExceptionXPathNavigator.SetValue 引发 NotSupportedException
【发布时间】:2009-10-21 15:48:06
【问题描述】:

我有以下代码,其最后一行在每次执行时都会导致 NotSupportedException,但我还没有找到解决方法。这个假设的类似代码查找具有特定标题的“书”,目的是将其更新为新标题。它确实找到了正确的节点,但未能更新它。

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

node.SetValue( "NewTitle" );

任何帮助将不胜感激。

【问题讨论】:

    标签: c# .net xml visual-studio-2005 xpath


    【解决方案1】:

    XPathNavigatorXPathDocument 对象创建的对象是只读的(请参阅MSDN: Remarks
    它应该使用XmlDocument 创建以便可编辑:

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

    【讨论】:

    • 但是如何使用 XPath 表达式从 XMLDocument 中查找节点?
    • 没关系,它是相同的 CreateNavigator() 方法。我之前没有注意到这一点。太棒了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多