documents:proglang:powershell:powershell-010
文書の過去の版を表示しています。
005.XML/JSON整形再び
2026-07-30
短めスニペットで記録
XML整形
シンプルにしたもの。
- fmtxml.ps1
param( [Parameter(Mandatory)] [string]$Path ) $xml = New-Object System.Xml.XmlDocument $xml.PreserveWhitespace = $false $xml.Load($Path) $directory = Split-Path $Path -Parent $fileName = [System.IO.Path]::GetFileNameWithoutExtension($Path) $newPath = Join-Path $directory "${fileName}_new.xml" $settings = [System.Xml.XmlWriterSettings]::new() $settings.Indent = $true $settings.Encoding = [System.Text.UTF8Encoding]::new($false) # BOMなし $writer = [System.Xml.XmlWriter]::Create($newPath, $settings) try { $xml.Save($writer) } finally { $writer.Dispose() }
JSON整形
シンプルにしたもの。
- fmtjson.ps1
param( [Parameter(Mandatory)] [string]$Path ) $json = Get-Content $Path -Raw | ConvertFrom-Json $directory = Split-Path $Path -Parent $fileName = [System.IO.Path]::GetFileNameWithoutExtension($Path) $newPath = Join-Path $directory "${fileName}_new.json" $jsonText = $json | ConvertTo-Json -Depth 100 # UTF-8(BOMなし)を明示して保存するため WriteAllText() を使用。 # Set-Content / Out-File は PowerShell バージョンによって既定エンコーディングの差異があるため、 # スニペットとしては .NET API を直接利用する。 [System.IO.File]::WriteAllText( $newPath, $jsonText, [System.Text.UTF8Encoding]::new($false) )
documents/proglang/powershell/powershell-010.1785368794.txt.gz · 最終更新: by k896951
