Home > Enterprise >  How to insert an app.config section or change it if it is already there
How to insert an app.config section or change it if it is already there

Time:04-11

I have the following simplified app.config file of a .NET 4.7 application:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
   </startup>
   <runtime>
   </runtime>
</configuration>

Now, I need to insert a new section (element) named Dependencies into it if that section does not exist yet. That section needs to have a child element named MyDependencies which can contain several CustomDependency elements. Here's an example:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
   </startup>
   <Dependencies>
      <MyDependencies>
         <CustomDependency Name="UniqueName" Id="12345678-1234-1234-1234-123456781234" AnotherAttribute="CustomValue"/>
      </MyDependencies>
   </Dependencies>
   <runtime>
   </runtime>
</configuration>

However, if the CustomDependency element with a specific name is already there, the values of the attributes of that element (Id and AnotherAttribute) just need to be changed. This should be done by using parameters that are submitted to the XSLT file.

So, the input might look like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
   </startup>
   <Dependencies>
      <MyDependencies>
        <CustomDependency Name="UniqueName" Id="ABC45678-1234-1234-1234-123456781234" AnotherAttribute="WrongValue"/>
        <CustomDependency Name="UniqueName2" Id="87654321-1234-1234-1234-123456781234" AnotherAttribute="CustomValue2"/>
      </MyDependencies>
      <YourDependencies>
        <CustomDependency Name="UniqueName2" Id="ABC54321-1234-1234-1234-123456781234" AnotherAttribute="CustomValue2"/>
      </YourDependencies>
   </Dependencies>
   <runtime>
   </runtime>
</configuration>

in which case the attribute values of the element with the name UniqueName, e.g.

<CustomDependency Name="UniqueName" Id="ABC45678-1234-1234-1234-123456781234" AnotherAttribute="WrongValue"/>

have to be updated to

<CustomDependency Name="UniqueName" Id="12345678-1234-1234-1234-123456781234" AnotherAttribute="CustomValue"/>

How would the XSLT code need to look like to make this happen?

CodePudding user response:

Try this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes" />
  
  <xsl:template match="@*|node()" name="copy-all">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/configuration/Dependencies/MyDependencies/CustomDependency[@Name='UniqueName']/@ProductCode">
    <xsl:attribute name="Id">CustomValue</xsl:attribute>
  </xsl:template>

  <xsl:template match="/configuration/Dependencies/MyDependencies/CustomDependency[@Name='UniqueName']/@SubFolder">
    <xsl:attribute name="AnotherAttribute">CustomValue</xsl:attribute>
  </xsl:template>

  <xsl:template match="startup[not(../Dependencies)]">
    <xsl:call-template name="copy-all" />
    <xsl:element name="Dependencies">
      <xsl:element name="MyDependencies">
        <xsl:element name="CustomDependency">
          <xsl:attribute name="Name">UniqueName</xsl:attribute>
          <xsl:attribute name="Id">12345678-1234-1234-1234-123456781234</xsl:attribute>
          <xsl:attribute name="AnotherAttribute">CustomValue</xsl:attribute>  
        </xsl:element>
      </xsl:element>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
  • Related