Powershell XMLDocument保存为无BOM的UTF-8

2024-05-14

我构建了一个 System.Xml.XmlDocument 类型的 XML 对象。

$scheme.gettype()
IsPublic IsSerial Name BaseType                                                         
-------- -------- ---- --------                                                         
True     False    XmlDocument System.Xml.XmlNode 

我使用 save() 方法将其保存到文件中。

$scheme.save()

这将以带有 BOM 的 UTF-8 格式保存文件。 BOM 会导致其他脚本出现问题。

当我们在 Notepad++ 中打开 XML 文件并将其保存为 UTF-8(不含 BOM)时,其他脚本不会出现问题。所以我被要求保存没有 BOM 的脚本。

The 保存方法的 MS 文档 https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.save?view=netframework-4.8 states:

编码属性的值取自 XmlDeclaration.Encoding 属性。如果 XmlDocument 没有 XmlDeclaration,或者 XmlDeclaration 没有编码属性,则保存的文档也不会具有编码属性。

The 有关 XmlDeclaration 的 MS 文档 https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldeclaration.encoding?view=netframework-4.8列出了 UTF-8、UTF-16 等的编码属性。它没有提到 BOM。

XmlDeclaration 是否具有省略 BOM 的编码属性?

附言。此行为在 Powershell 5 和 Powershell 7 中是相同的。


Unfortunately, the presence of an explicit encoding="utf-8" attribute in the declaration of an XML document causes .NET's [xml] (System.Xml.XmlDocument) type to .Save() https://learn.microsoft.com/en-US/dotnet/api/System.Xml.XmlDocument.Save the document, when given a file path, to an UTF-8-encoded file with BOM, which can indeed cause problems (even though it shouldn't[1]).

请求更改此设置 https://github.com/dotnet/runtime/issues/28218已经原则上开绿灯, 但是截至 .NET 6.0 尚未实现(由于关于改变的更大讨论[System.Text.Encoding]::UTF8 to not在这种情况下使用 BOM.Save()也将不再自动创建 BOM)。

有点讽刺的是,the absence of an encoding属性原因.Save()创建 UTF-8 编码的文件without a BOM.

A simple solution is therefore to remove the encoding attribute[2]; e.g.:

# Create a sample XML document:
$xmlDoc = [xml] '<?xml version="1.0" encoding="utf-8"?><foo>bar</foo>'

# Remove the 'encoding' attribute from the declaration.
# Without this, the .Save() method below would create a UTF-8 file *with* BOM.
$xmlDoc.ChildNodes[0].Encoding = $null

# Now, saving produces a UTf-8 file *without* a BOM.
$xmlDoc.Save("$PWD/out.xml")

[1] Per the XML W3C Recommendation https://www.w3.org/TR/xml/#charencoding: "entities encoded in UTF-8 MAY begin with the Byte Order Mark" [BOM].

[2] This is safe to do, because the XML W3C Recommendation https://www.w3.org/TR/xml/#charencoding effectively mandates UTF-8 as the default in the absence of both a BOM and an encoding attribute.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Powershell XMLDocument保存为无BOM的UTF-8 的相关文章

随机推荐

  • “未定义的行为”是否会扩展到编译时?

    我们都听过这样的警告 如果你调用未定义的行为在 C 或 C 中 任何事情可以发生 这是否仅限于任何运行时行为 或者这还包括任何编译时行为吗 特别是 编译器在遇到调用未定义行为的构造时是否允许拒绝代码 在标准中没有其他要求的情况下 甚至崩溃
  • 渲染:带参数的动作

    我有一个有 2 种方法的类 第一个方法由视图使用一些 GET 参数 params page 调用 我想保存这些参数并通过渲染操作将它们发送到我的第二个方法 class exemple def first sql save of params
  • 在 Qt5 中,是否需要 Q_INVOKABLE 来从 QML 调用公共 QObject 函数?

    我刚刚意识到我可以调用暴露于 QML 的对象的几乎任何函数 现在我对 Q INVOKABLE 很好奇 Qt5docs http doc qt io qt 5 qtqml cppintegration exposecppattributes
  • 获取 int() 参数必须是字符串或数字,而不是“Column”- Apache Spark

    如果我使用以下代码 我会收到此异常 int argument must be a string or a number not Column df df withColumn FY F when df ID substr 5 2 isin
  • 如何避免 NSNumberFormatter 中的四舍五入

    我试图拥有一个最大精度为 2 位小数的数字字符串 而其余小数只是被修剪掉而不是四舍五入 例如 I have 123456 9964 I want 123456 99 gt Just want to trim rest of the deci
  • R-在多个图的外缘绘制居中图例

    我想在具有多个绘图的设备中的绘图区域之外绘制居中图例 SO 中提出了许多关于更改 R 图中图例位置的问题 略有不同 例如 1 R 组合图的通用标题和图例 https stackoverflow com questions 8736966 r
  • 当测试集中不存在响应变量时,h2o 预测有时会失败

    当在不存在响应变量的测试集上进行预测时 如果在训练中对因子变量使用一种热编码 则 h2o 会以各种不同的方式失败 无论是在训练 GLM 时隐式指定还是在其他方法中显式指定时 R 3 4 0 和 h2o 3 12 0 1 中存在此错误 我们还
  • Scala中有类似Java Stream的“peek”操作吗?

    在Java中你可以调用peek x gt println x 在 Stream 上 它将对每个元素执行操作并返回原始流 这与 foreach 不同 foreach 是 Unit Scala 中是否有类似的东西 最好是适用于所有 Monady
  • 从 TypeScript 运行任何 Linux 终端命令?

    有没有办法直接从 TypeScript 类中执行 Linux 终端命令 这个想法是做类似的事情 let myTerminal new LinuxTerminal let terminalResult myTerminal run sudo
  • 将类转换为 JSONObject

    我有好几堂这样的课 我想将类转换为 JSONObject 格式 import java io Serializable import com google gson annotations SerializedName public cla
  • 优化 R 中的嵌套 for 循环

    我尝试加速下面的代码 但没有成功 我读到Rfast https cran r project org web packages Rfast Rfast pdf包 但我也未能实现该包 有没有办法优化R中的以下代码 RI lt function
  • sleep 0 有特殊含义吗?

    我看到很多用法sleep 0在我的一个客户项目中 代码看起来像这样 while true sleep 0 end 阅读一些像这样的答案this https stackoverflow com questions 3727420 signif
  • Karasuba算法递归过多

    我正在尝试用 c 实现 Karasuba 乘法算法 但现在我只是想让它在 python 中工作 这是我的代码 def mult x y b m if max x y lt b return x y bm pow b m x0 x bm x1
  • 根据传递的参数覆盖 Javascript 函数

    是否可以根据传递给函数的参数数量来重写函数 例如 function abc name document write My name is name function abc name friend document write My nam
  • Maven 按顺序构建所有内容

    我有一个项目 其中所有项目都有一个父 pom 定义如下
  • dask apply:AttributeError:“DataFrame”对象没有属性“name”

    我有一个参数数据框 并对每一行应用一个函数 该函数本质上是几个 sql queries 和对结果的简单计算 我正在尝试利用 Dask 的多处理 同时保持结构和界面 下面的例子有效并且确实有显着的提升 def get metrics row
  • 使用 FoldLine 解析多个块

    对于这个简化的问题 我试图解析一个如下所示的输入 foo bar baz quux woo hoo xyzzy glulx into foo bar baz quux woo hoo xyzzy glulx 我尝试过的代码如下 import
  • 如何处理 clang 中的全局构造函数警告?

    Clang 警告 当使用 Weverything or Wglobal constructors 关于静态对象的构造函数 warning declaration requires a global constructor Wglobal c
  • PHP file_exists() 对我不起作用?

    由于某种原因 下面的 PHP 代码将无法工作 我无法弄清楚 很奇怪的是 file exists 似乎没有看到图像确实存在 我已经检查以确保将良好的文件路径插入到 file exists 函数中并且它仍在运行 如果我将 file exists
  • Powershell XMLDocument保存为无BOM的UTF-8

    我构建了一个 System Xml XmlDocument 类型的 XML 对象 scheme gettype IsPublic IsSerial Name BaseType True False XmlDocument System Xm