问题
I'm using Powershell with to read in and modify a web.config file, all operations are executing successfully but I am losing indentation and new breaks in multiline attributes upon saving the file. Here is what the web.config file looks like prior to editing:
<maintenance isSet="false" startDate="2012-03-07T00:00:00" endDate="2012-03-07T23:59:59">
<allowedIPAddresses>
<add name="Johns Machine" address = "xx.xx.xxx.xx" />
<add name="Marys Machine" address = "xx.xx.xxx.xx" />
</allowedIPAddresses>
Here is the Powershell code to edit the xml:
$script:myXML = New-Object System.Xml.XmlDocument
$myXML.PreserveWhitespace = $true
$myXML.CreateXmlDeclaration("1.0","iso-8859-1",$null)
$myXML.Load("C:\dev\web.config")
$ipEntries = $myXML.SelectNodes("/maintenance/allowedIPAddresses")
foreach ($ipEntry in $ipEntries)
{
$ipEntry.RemoveAll()
}
$reader = new-object System.IO.StreamReader($ipTextFile)
$i = 0
while(($line = $reader.ReadLine()) -ne $null)
{
$i++
$node = $myXML.CreateElement("add")
$node.SetAttribute("name", "Machine " + ($i))
$node.SetAttribute("address", $line)
$myXML.SelectSingleNode("/configuration/maintenance/allowedIPAddresses").AppendChild($node)
}
$myXML.Save("C:\dev\web.config")
Here is the file after the script is run
<maintenance isSet="false" startDate="2012-03-07T00:00:00" endDate="2012-03-07T23:59:59">
<allowedIPAddresses><add name="Johns Machine" address="xx.xx.xxx.xx" /><add name="Marys Machine" address="xx.xx.xxx.xx" /></allowedIPAddresses>
Is there a method of preserving line breaks within multiline attributes and keeping indentation after the file has been saved?
回答1:
Setting up formatting requires using XmlWriterSettings and XmlWriter classes. The former sets up formatting like indent, newlines and such. The latter is used to write the document. Both are available in System.Xml namespace. They are easy enough to use in Powershell. Like so,
# Valid XML for example's sake
[xml]$doc = @'
<root>
<maintenance isSet="false" startDate="2012-03-07T00:00:00" endDate="2012-03-07T23:59:59">
<allowedIPAddresses>
<add name="Johns Machine" address = "xx.xx.xxx.xx" />
<add name="Marys Machine" address = "xx.xx.xxx.xx" />
</allowedIPAddresses>
</maintenance>
</root>
'@
# Let's add Bob's machine. Create an element and add attributes
$node = $doc.CreateElement("add")
$node.SetAttribute("name", "Bobs Machine")
$node.SetAttribute("address", "yy.yy.yyy.yy")
$doc.root.maintenance.allowedIPAddresses.AppendChild($node)
# Set up formatting
$xwSettings = new-object System.Xml.XmlWriterSettings
$xwSettings.indent = $true
$xwSettings.NewLineOnAttributes = $true
# Create an XmlWriter and save the modified XML document
$xmlWriter = [Xml.XmlWriter]::Create("c:\temp\newlines.xml", $xwSettings)
$doc.Save($xmlWriter)
Output (Markup removes the indents, though):
<?xml version="1.0" encoding="utf-8"?>
<root>
<maintenance
isSet="false"
startDate="2012-03-07T00:00:00"
endDate="2012-03-07T23:59:59">
<allowedIPAddresses>
<add
name="Johns Machine"
address="xx.xx.xxx.xx" />
<add
name="Marys Machine"
address="xx.xx.xxx.xx" />
<add
name="Bobs Machine"
address="yy.yy.yyy.yy" />
</allowedIPAddresses>
</maintenance>
</root>
来源:https://stackoverflow.com/questions/17966602/preserve-mutliline-attributes-and-indentations-with-xml-document-in-powershell