Preserve mutliline attributes and indentations with xml.document in Powershell

。_饼干妹妹 提交于 2019-12-22 08:37:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!