Reputation: 11947
I'm in PowerShell, manipulating xml documents (.NET's System.Xml.XmlDocument class). I want to add custom white space between xml attributes. I can't find an API call for it in .NET, and nobody online seems to be trying to do this. Here is a simplified example of what I'm trying to do, but with a comment where I don't know what to call:
$xmlDoc = [xml]@"
<root>
<test Id="1" a="a" b="b" />
<test Id="2" a="a" b="b" />
</root>
"@
$elements = @($xmlDoc.SelectNodes('//*'))
foreach($element in $elements)
{
$attributes = $element.Attributes
foreach($attribute in $attributes)
{
#
# How do I access the whitespace around the attributes?
#
}
}
# Output to screen exactly what will be saved to disk
$tempFile = [System.IO.FileInfo]([System.IO.Path]::GetTempFileName())
$xmlDoc.save($tempFile)
foreach($line in (Get-Content $tempFile))
{ Write-Host $line }
Remove-Item $tempFile
Does anyone know how to access the whitespace around a System.Xml.XmlAttribute?
Upvotes: 1
Views: 965
Reputation: 6101
Unfortunately, there is no way to do it using XmlDocument (and related) or XDocument (and related). Even such low level thing like XmlTextWriter does not allow you to add additional whitespaces or new lines between attributes.
Every XmlNode/XObject serializable, so you can write your own special-formatted-serialization or post-process the resulting XML after the standard serialization process. Hope this helps
Upvotes: 3