【问题标题】:Little utility to add element in .config app file在 .config 应用程序文件中添加元素的小实用程序
【发布时间】:2016-05-04 02:10:42
【问题描述】:

我需要制作一个小实用程序来升级该应用程序之外的 xml .config c# 应用程序文件。

我只需要添加这个元素:

<?xml version="1.0"?>
  <configuration>

  ...

  ...

  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
</configuration>

哪种方法最简单?

【问题讨论】:

  • 将文件读取为 XML 文档并添加为节点。

标签: c# xml app-config


【解决方案1】:

我现在不知道您要添加什么,但这是一个示例

试试这样:

来源:

<?xml version="1.0"?>
<configuration>
    ...

</configuration>

源代码 [解决方案已经过测试,可以根据需要运行]

        var xmlDoc = new XmlDocument();

        xmlDoc.Load("test.xml");

        var startupNode = xmlDoc.SelectSingleNode("/configuration/startup");

        var configuratioNode = xmlDoc.SelectSingleNode("/configuration");

        if (startupNode == null)
        {
            var newNode = xmlDoc.CreateElement("startup");

            newNode.SetAttribute("useLegacyV2RuntimeActivationPolicy", "true");

            var secondNewNode = xmlDoc.CreateElement("supportedRuntime");

            secondNewNode.SetAttribute("version", "v4.0");
            secondNewNode.SetAttribute("sku", ".NETFramework,Version=v4.5.2");

            newNode.AppendChild(secondNewNode);

            configuratioNode.AppendChild(newNode);
        }
        else
        {
            var supportedRuntime = startupNode.SelectSingleNode("/supportedRuntime");

            if (supportedRuntime == null)
            {
                var secondNewNode = xmlDoc.CreateElement("supportedRuntime");

                secondNewNode.SetAttribute("version", "v4.0");
                secondNewNode.SetAttribute("sku", ".NETFramework,Version=v4.5.2");

                startupNode.AppendChild(secondNewNode);
            }
        }

        xmlDoc.Save("test.xml");

输出:

<?xml version="1.0"?>
<configuration>

  ...

  ...




 <startup useLegacyV2RuntimeActivationPolicy="true">
 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
 </startup>

 </configuration>

【讨论】:

  • 好!这正是我需要的!有没有办法在尝试添加之前检查具有“supportedRuntime”属性的“启动”部分是否已经存在?
  • 如果文档中已经出现类似“ ”?如何编辑它以替换为“”?
【解决方案2】:

我会使用 XSLT。示例:

来源

<?xml version="1.0"?>
<configuration>
    <someXml/>
</configuration>

转型

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="configuration">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <startup useLegacyV2RuntimeActivationPolicy="true">
                <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
            </startup>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

目标

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <someXml/>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>
</configuration>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2016-08-20
    相关资源
    最近更新 更多