xml


Removing XML Node In PowerShell Removes Extra Tags That I Need


I have an XML file in the following format:
<?xml version="1.0" encoding="utf-8"?>
<factory>
<map>
<add key="Audit" assembly="Python.Platform.Core.dll" class="Python.Core.Audit.Services.Audit">
<parameter name="configFilePath" class="string" value="Config\Audit\AuditConfiguration.xml" />
</add>
<add key="Security.Password" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Security.Services.PasswordSecurityMD5">
</add>
<add key="Security.Password.Local" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Security.Services.PasswordSecurityMD5">
</add>
<add key="Guest.Registration" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Guest.Services.DbGuestRegistration">
<parameter name="registrationStoredProcedure" class="string" value="AddNewGuest" />
<parameter name="updateEncodedKeyStoredProcedure" class="string" value="UpdateGuestEncodedKey" />
<parameter name="unregistrationStoredProcedure" class="string" value="DeleteGuest" />
<parameter name="DeleteExpiredGuestStoredProcedure" class="string" value="DeleteExpiredGuest" />
<parameter name="defaultValidDays" class="int" value="30" />
</add>
</map>
</factory>
My goal is to remove this section:
<add key="Audit" assembly="Python.Platform.Core.dll" class="Python.Core.Audit.Services.Audit">
<parameter name="configFilePath" class="string" value="Config\Audit\AuditConfiguration.xml" />
</add>
The PowerShell Script I'm running is as follows:
$path = "C:\testing\myxml.xml"
[Xml]$myxml = Get-Content -Path $path -Raw
$old = $myxml.SelectSingleNode("/factory/map/add[#key='Audit']")
$parent = $old.ParentNode
[void] $parent.RemoveChild($old)
$myxml.save($path)
Unfortunately, when I run it, it's also removing any closing tag from any Add node that does not have a paramet child node. So instead of just deleting the section I want to delete (which it is doing), it now looks like this:
<?xml version="1.0" encoding="utf-8"?>
<factory>
<map>
<add key="Security.Password" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Security.Services.PasswordSecurityMD5">
<add key="Security.Password.Local" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Security.Services.PasswordSecurityMD5">
<add key="Guest.Registration" assembly="OpenContent.Platform.Core.dll" class="OpenContent.Core.Guest.Services.DbGuestRegistration">
<parameter name="registrationStoredProcedure" class="string" value="AddNewGuest" />
<parameter name="updateEncodedKeyStoredProcedure" class="string" value="UpdateGuestEncodedKey" />
<parameter name="unregistrationStoredProcedure" class="string" value="DeleteGuest" />
<parameter name="DeleteExpiredGuestStoredProcedure" class="string" value="DeleteExpiredGuest" />
<parameter name="defaultValidDays" class="int" value="30" />
</add>
</map>
</factory>
Any suggestions on how to get this to run without removing the extra closing tags?
This seems to be the default behavior of XmlElement.WriteTo() and similar methods - using shortform tags for any leaf elements that have no inner non-xml content.
The way this is determined is by inspecting the IsEmpty property of each element, so you can trick a writer into preserving longform tags by setting IsEmpty to $false before calling Save():
# Locate all <add /> nodes with no child elements
$addLeafNodes = $myxml.SelectNodes('//add[not(child::*)]')
# Loop through them
foreach($node in $AddLeafNodes){
# Set IsEmpty property to false
$node.IsEmpty = $false
}
# Save document
$myxml.Save($path)

Related Links

Unwanted “US” character in XML (not Escape-Characters, no “nul”)
XML/JSON processing
Updating config xml file and save it using powershell
XSLT transform moving namespace
How to link XSLT from another server on XML
Editing attributes in LuaXML
understanding position() function in XSLT
get element names from unmarshalled object class
convert graph represented by xml to visual representation
VB.NET XmlTextReader bytes read
VB.Net XML Parsing Issue
XSL: Generate a global integer id that increments itself
Failed to read schema document [XSD] because
String to date in XSLT 2.0: error using xs:date
How do I produce the required XML output grouping and sorting by like source nodes?
error in calling web service through Internet Explorer

Categories

HOME
phantom-dsl
deep-learning
pyqt
nuget
tivoli
nsbundle
permissions
dojo
pyqt4
crystal-reports-2013
opengl-3
computer-science
rcloud
project
mql5
siddhi
http-post
null-pointer
red5
google-container-registry
identity-management
sse
href
tosca
zxing
perlbrew
dapper-extensions
syntax-highlighting
openmdao
realm-mobile-platform
twiml
sqsh
alm
livelink
fossil
nested-lists
pdw-file-browser
apic
apns-php
opensmpp
numberpicker
android-softkeyboard
linqpad
http-request
flask-login
getchar
httrack
serde
identification
remedy
java-bytecode-asm
colorbar
git-flow
rackspace-cloud
gpg-signature
shinobi
webjars
async.js
tinkerpop3
spring-cloud-feign
flex4.6
mta
wso2developerstudio
appscale
jtwig
database-relations
halcon
mobiledoc-kit
asyncdisplaykit
dllimport
microsoft-reporting
amiga
snoop
typeconverter
git-ftp
zxspectrum
xmltype
procedural-programming
ejb-2.x
rapidsvn
cosine-similarity
administration
appserver
human-computer-interface
rmysql
nsmenu
jqmodal
unix-socket
drupal-nodes
mt4j
mongohq
merb
abstract-data-type
stacky
device-width
extreme-programming
ilasm
convention
flex-mobile
screen-size
android-holo-everywhere
cuteeditor
msr
photolibrary
diagrams
zen
build-environment
feasibility
pbcopy
appliance

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile