XDocument更改所有属性名称

3

I have an XDocument that looks similar to

<root>
     <a>
          <b foo="1" bar="2" />
          <b foo="3" bar="4" />
          <b foo="5" bar="6" />
          <b foo="7" bar="8" />
          <b foo="9" bar="10" />
     </a>
</root>

我希望将属性foo更改为其他内容,将属性bar更改为其他内容。我该如何轻松实现这一点?我的当前版本(如下所示)在处理大型文档时会导致堆栈溢出,并且有一种难闻的味道。

        string dd=LoadedXDocument.ToString();
        foreach (var s in AttributeReplacements)
            dd = dd.Replace(s.Old+"=", s.New+"=");

好问题(+1)。请查看我的答案,其中包含完整的XSLT解决方案——可能是最简单的之一。 - Dimitre Novatchev
2个回答

3

使用文本搜索和替换进行此操作应该使用StringBuilder,以避免在循环中创建字符串的常规问题(大量垃圾数据)。还很难防止误报(如果匹配属性的文本出现在文本节点中怎么办?)

更好的选择包括:

  1. 加载到XDocument或XmlDocument中,通过遍历树替换匹配的属性。
  2. 使用XSLT
  3. 从XmlReader读取并直接写入XmlWriter,更改属性。

其中#3避免将整个文档加载到内存中。 #2需要XSLT技能,但可以轻松地允许任意数量的替换(核心可以是模板,在运行时注入新的旧属性对)。 #1可能是最简单的,但要将整个文档放在内存中,并处理多个替换的开销。

我可能会看看XSLT与Xml Reader / Writer方法作为备选方案。

然而,#1应该是最简单实现的方法,类似于以下内容(忽略XML命名空间等细节):

using System.Xml.Linq;
using System.Xml.XPath;

var xdoc = XDocument.Load(....);
var nav = xdoc.CreateNavigator();

foreach (repl in replacements) {
  var found = (XPathNodeIterator) nav.Evaluate("//@" + repl.OldName);

  while (found.MoveNext()) {
    var node = found.Current;
    var val = node.Value;
    node.DeleteSelf(); // Moves ref to parent.
    node.CreateAttribute("", repl.NewName, "", val);
  }
}

最终的选择将取决于平衡性能(特别是处理大型文档时的内存)和复杂性。但只有您(和您的团队)可以做出这个决定。

2

这里是一个完整的XSLT解决方案:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:my="my:reps"
    exclude-result-prefixes="my"
>
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <my:replacements>
      <foo1 old="foo"/>
      <bar1 old="bar"/>
    </my:replacements>

    <xsl:variable name="vReps" select=
     "document('')/*/my:replacements/*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:variable name="vRepNode" select=
   "$vReps[@old = name(current())]"/>

   <xsl:variable name="vName" select=
    "name(current()[not($vRepNode)] | $vRepNode)"/>

   <xsl:attribute name="{$vName}">
     <xsl:value-of select="."/>
   </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

当对提供的XML文档应用此转换时,将产生所需的结果。
<root>
   <a>
      <b foo1="1" bar1="2"/>
      <b foo1="3" bar1="4"/>
      <b foo1="5" bar1="6"/>
      <b foo1="7" bar1="8"/>
      <b foo1="9" bar1="10"/>
   </a>
</root>

请注意,这是一个通用的解决方案,可以指定和修改任何替换列表而不需要修改代码。这些替换可以在一个单独的XML文件中进行维护,以方便维护。

非常感谢您的努力。 - Dested

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接