我正在尝试读取 .xml 文件,更改一些值(尚未),然后将其写回。在不做任何改变的情况下,我希望得到和进去的一样的东西。它没有。
PS H:\src\tws> type .\test000.xml
<?xml version="1.0"?>
<eventRuleSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules"
xsi:schemaLocation="http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules/EventRules.xsd">
<eventRule name="PW-TEST001" ruleType="filter" isDraft="no">
<description>Paul's test001</description>
</eventRule>
</eventRuleSet>
这是我用来读入和写出的简单代码。
PS H:\src\tws> Get-Content .\con000.ps1
$x = [xml](Get-Content -Path .\test000.xml)
$x | Export-Clixml -Path .\con000.xml -Encoding utf8
输出有 和 部分。这是为什么?我想知道里面的内容。我不关心换行符或 HTML 实体的使用。我只想让内容成为精髓。是的,计划是读取一个模板,更改一些值,然后输出一个新的 .xml 文件。这将输入到 IBM/HCS Workload Scheduler。
PS H:\src\tws> type .\con000.xml
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<XD><?xml version="1.0"?><eventRuleSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules" x
si:schemaLocation="http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules http://www.ibm.com/xmlns/prod/tws/1.0/event-management/rules/EventRules.xsd"><eventRule
name="PW-TEST001" ruleType="filter" isDraft="no"><description>Paul's test001</description></eventRule></eventRuleSet></XD>
</Objs>
最佳答案
Export-CliXml
的目的|是通过 Import-CliXml
序列化任意对象以供以后反序列化,使用关于保留特定输入类型以供以后通过 Import-CliXml
“补水”的尽力而为的表示. [xml]
的文本表示文件到一个文件。 为了保存
[xml]
的文本表示实例到一个文件,你有两个基本的选择:.OuterXml
在(修改)[xml]
实例并将其发送到文件 - 通过 Set-Content
或通过Out-File
/>
,但请注意这些 cmdlet 在 Windows PowerShell 中应用的不同默认字符编码。 [xml]
type's .Save()
method保存到文件时可以方便地执行隐式 pretty-print ,但存在以下缺陷:encoding
的 XML 声明的情况下属性,该方法创建一个没有 BOM 的 UTF-8 文件(从跨平台的角度来看这是更可取的)。 encoding="UTF-8"
的 XML 声明存在,从 .NET Core 2.1/.NET v4.7 开始,生成的文件将是带有 BOM 的 UTF-8;见 this GitHub issue . [System.Xml.XmlWriter]
具有显式创建的文件流对象的实例,这更麻烦,但可以让您控制 pretty-print 格式的细节。 这是一个简单的例子,
.OuterXml
:# Read the input file into an XML document (in-memory DOM).
$x = [xml] (Get-Content -Raw ./test000.xml)
# Make updates to the in-memory document
$x.eventRuleSet.eventRule.description = 'new description'
# Save the modified document as text to an output file,
# using the un-prettied textual representation provided by the .OuterXml
# property.
# If *BOM-less* UTF-8 encoding is what you want, simply use
# $x.Save("$PWD/con000.xml")
# In PowerShell *Core*, you'd get BOM-less UTF-8 even with the command below.
$x.OuterXml | Set-Content -Encoding utf8 ./con000.xml
关于使用 BOM 的说明(又名 Unicode 签名)与 UTF-8和其他 Unicode 编码:
-Encoding utf8
总是创建一个 BOM(不仅适用于 Set-Content
,还适用于其他产生文件输出的 cmdlet,例如 Out-File
和 Export-Csv
)。-Encoding utf8
时);您可以选择使用 -Encoding utf8BOM
创建 BOM . 为了获得最佳的整体兼容性,应避免使用 UTF-8 文件中的 BOM:Unix 平台和 Windows 平台上也使用的 Unix-heritage 实用程序通常不知道如何处理它们。
同样,
-Encoding UTF7
应该避免,因为它不是标准的 Unicode 编码(并且在两个 PowerShell 版本中都没有 BOM 编写)。在两个 PowerShell 版本中,
-Encoding
提供的所有其他 Unicode 编码创建一个(适合编码的)BOM:Unicode
(UTF-16LE), bigendianunicode
(UTF-16BE) 和 utf32
(UTF-32)。
关于xml - 如何将 .xml 读入内存并写出相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53000457/