首页 / C# / C#-XDocument更改所有属性名称
C#-XDocument更改所有属性名称
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-XDocument更改所有属性名称,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1444字,纯文字阅读大概需要3分钟。
内容图文
![C#-XDocument更改所有属性名称](/upload/InfoBanner/zyjiaocheng/688/f65ff64b5267498b8bf191699daad34c.jpg)
我有一个看起来像的XDocument
<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更改为其他内容,并将属性栏更改为其他内容.我如何轻松地做到这一点?我当前的版本(下面)堆栈中充满了大型文档,并且闻起来很可怕.
string dd=LoadedXDocument.ToString();
foreach (var s in AttributeReplacements)
dd = dd.Replace(s.Old+"=", s.New+"=");
解决方法:
这是完整的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文件中,以便于维护.
内容总结
以上是互联网集市为您收集整理的C#-XDocument更改所有属性名称全部内容,希望文章能够帮你解决C#-XDocument更改所有属性名称所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。