François Racine
François Racine

Reputation: 21

Adding a line in XML with a space

I have that XML

    <application>
  <component name="UpdatesConfigurable">    
    <enabledExternalComponentSources>
      <item value="Android SDK" />
    </enabledExternalComponentSources>
    <option name="externalUpdateChannels">
      <map>
        <entry key="Android SDK" value="Stable Channel" />
      </map>
    </option>
    <knownExternalComponentSources>
      <item value="Android SDK" />
    </knownExternalComponentSources>
    <option name="LAST_BUILD_CHECKED" value="IU-202.7660.26" />
    <option name="LAST_TIME_CHECKED" value="1606406082514" />
  </component>
</application>

I need to add

<option name="CHECK_NEEDED" value="false" />

Then getting:

    <application>
  <component name="UpdatesConfigurable">
    <option name="CHECK_NEEDED" value="false" />
    <enabledExternalComponentSources>
      <item value="Android SDK" />
    </enabledExternalComponentSources>
    <option name="externalUpdateChannels">
      <map>
        <entry key="Android SDK" value="Stable Channel" />
      </map>
    </option>
    <knownExternalComponentSources>
      <item value="Android SDK" />
    </knownExternalComponentSources>
    <option name="LAST_BUILD_CHECKED" value="IU-202.7660.26" />
    <option name="LAST_TIME_CHECKED" value="1606406082514" />
  </component>
</application>

I created that script:

    $fileName = “C:\sovgarde\updates.xml”;
$xml = [System.Xml.XmlDocument](Get-Content $fileName);

$child = $xml.CreateElement('option name')
$child.SetAttribute('CHECK_NEEDED','false')
$node = $xml.SelectSingleNode('component name')
$node.AppendChild($child)

But getting a failure at "option name"...

How adding that line if its not already existing. I believe the issue is in the space.

Thanks,

Upvotes: 1

Views: 272

Answers (3)

Fran&#231;ois Racine
Fran&#231;ois Racine

Reputation: 21

This will be working:

$fileName = “C:\sovgarde\updates.xml”;
$xml = [System.Xml.XmlDocument](Get-Content $fileName);

$child = $xml.CreateElement('option')
$child.SetAttribute('name', 'CHECK_NEEDED')
$child.SetAttribute('value','false')
$node = $xml.SelectSingleNode('//component')
$node.AppendChild($child)
$node
$xml.Save($fileName)

Now I am trying to remove the attribute check_needed and it is not working

   $fileName = “C:\sovgarde\updates.xml”;
$xml = [System.Xml.XmlDocument](Get-Content $fileName);

$child = $xml.CreateElement('option')
$child.SetAttribute('name', 'CHECK_NEEDED')
$child.SetAttribute('value','false')
If ($xml.application.component.option.name -icontains "CHECK_NEEDED") {
    
    $node = $xml.SelectSingleNode('//component')
    $node.RemoveAttribute('CHECK_NEEDED') 
    $xml.Save($fileName)   
    
}

The detection is working correctly but after the save the value is still existing.

Upvotes: 0

Yitzhak Khabinsky
Yitzhak Khabinsky

Reputation: 22283

Here is your Identity Transform XSLT.

It works in a very simple and powerful way. It will copy the input XML as-is. But when it gets to the <enabledExternalComponentSources> element, it will add a new element

<option name="CHECK_NEEDED" value="false" /> to a proper location. Which is exactly what you need.

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

   <!-- IdentityTransform -->
   <xsl:template match="/ | @* | node()">
         <xsl:copy>
               <xsl:apply-templates select="@* | node()" />
         </xsl:copy>
   </xsl:template>
   
   <xsl:template match="enabledExternalComponentSources">
        <option name="CHECK_NEEDED" value="false" />
         <xsl:copy>
               <xsl:apply-templates select="@* | node()" />
         </xsl:copy>
   </xsl:template>   

</xsl:stylesheet>

To see what it does: XSLT Fiddle

Simple Powershell script for XSLT

Upvotes: 0

Mads Hansen
Mads Hansen

Reputation: 66783

The element name is option, not option name.

You need to create an element named option and then you want to create two attributes. One named name with the value "CHECK_NEEDED" and another attribute named check with the value "false"

$fileName = “C:\sovgarde\updates.xml”;
$xml = [System.Xml.XmlDocument](Get-Content $fileName);

$child = $xml.CreateElement('option')
$child.SetAttribute('name', 'CHECK_NEEDED')
$child.SetAttribute('check','false')
$node = $xml.SelectSingleNode('component')
$node.AppendChild($child)

Upvotes: 1

Related Questions