xml - 避免使用powershell从xml文件中删除空行
问题描述
基本上问题是原始 xml 文件 ( $myFilePath
) 在文件中有空行,但是在保存 xml 文件时,空行被删除。
保存时如何避免从文件中删除空行?
以下是我尝试过的 -
$myXmlDocument = [xml] (Get-Content -Path $myFilePath) # File ready style-1
$myXmlDocument = [xml]([System.IO.File]::ReadAllText($myFilePath)) # File ready style-2
$myXmlDocument.PreserveWhitespace = $true #additional setting to preserver the format
#performing some string match and update. without the update also having same issue of blank line removal while saving.
$WadiAPConfigXmlDocument.Save($myFilePath) # saving the file
解决方案
您需要在加载文件之前PreserveWhitespace
设置属性:
# Create XmlDocument object and configure PreserveWhitespace
$xmlDoc = [xml]::new()
$xmlDoc.PreserveWhitespace = $true
# Load the document
$xmlDoc.Load($myFilePath)
# or
$xmlDoc.LoadXml($(Get-Content $myFilePath -Raw))
推荐阅读
- ios - SceneKit 节点的交集
- python - 根据字典在 csv 中搜索单词
- .net - 尝试在 Azure 管道 PATH ISSUE 上部署 SQL DB
- sql-server - 在sql的列中找出不包含特定值的字段
- javascript - 我想从数据库中获取图像路径并将其显示在反应组件中,我正在使用 require(),(错误:找不到模块)发生?
- javascript - 向下滚动时动画元素
- c# - 使用 Selenium C# 在私有模式下的 IE
- python - 如何在美丽的汤和python中指定css选择器?
- c# - 在 IAuthenticationService.SignInSync 之后无法立即读取身份声明
- machine-learning - 当 CPU 仅在 pytorch 中可用时,如何并行化训练循环样本?