在 Powershell 中使用 xml.document 保留多行属性和缩进 [英] Preserve mutliline attributes and indentations with xml.document in Powershell

查看:30
本文介绍了在 Powershell 中使用 xml.document 保留多行属性和缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Powershell 读入和修改 web.config 文件,所有操作都成功执行,但我在保存文件时丢失了缩进和多行属性中的新中断.以下是编辑前的 web.config 文件:

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>

这是用于编辑 xml 的 Powershell 代码:

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?

推荐答案

设置格式需要使用 XmlWriterSettings 和 XmlWriter 类.前者设置缩进、换行等格式.后者用于编写文档.两者都在 System.Xml 命名空间中可用.它们很容易在 Powershell 中使用.像这样,

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>

这篇关于在 Powershell 中使用 xml.document 保留多行属性和缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆