2017-05-24 6 views
0

私はさまざまな文字の通常のHTML名を含む入力XMLファイルを持っています。二重引用符= "などPowershell - xml

<Notes>Double Quote &quot; Single Quote &pos; Ampersand &amp;</Notes> 

<?xml version="1.0" encoding="UTF-8"?> 
<OrganisationUnits> 
    <OrganisationUnitsRow num="8"> 
    <OrganisationId>ACME24/7HOME</OrganisationId> 
    <OrganisationName>ACME LTD</OrganisationName> 
    <Notes>Double Quote &quot; Single Quote &pos; Ampersand &amp; </Notes> 
    <Sector>P</Sector> 
    <SectorDesc>Private Private &amp; Voluntary</SectorDesc> 
    </OrganisationUnitsRow> 
</OrganisationUnits> 

<?xml version="1.0" encoding="UTF-8"?> 
<OrganisationUnits> 
    <OrganisationUnitsRow num="8"> 
    <OrganisationId>ACME24/7HOME</OrganisationId> 
    <OrganisationName>ACME LTD</OrganisationName> 
    <Notes>Double Quote " Single Quote ' Ampersand &</Notes> 
    <Sector>P</Sector> 
    <SectorDesc>Private Private & Voluntary</SectorDesc> 
    </OrganisationUnitsRow> 
</OrganisationUnits> 

た後、私はXMLとしてファイルを扱っておりますし、それは何も、非常に凝った、[OK]を処理します。 &quot;のような出力が保存されている

$xml = [xml](Get-Content $path\$File) 
foreach ($CMCAddressesRow in $xml.OrganisationUnits.OrganisationUnitsRow) { 
    blah 
    blah 
} 
$xml.Save("$path\$File") 

すべてのHTMLコードが"に置き換えられます。 元のHTML &quot;文字はどのように保持できますか?さらに重要なのはなぜそれが起こっているかです。

+0

XMLファイルの6行目に '&apos'が必要ですか? – lit

+0

System.Net.WebUtility.HtmlDecodeとSystem.Net.WebUtility.HtmlEncode – jdweng

+0

"と&aposの置換は、ファイルが[xml]として読み取られたときに既に発生しているようです。 – lit

答えて

1

あなたが言及しているのは「文字エンティティ」です。 PowerShellはインポート時にそれらを変換するので、これらのエンティティが表す実際の文字を扱うことができ、エクスポート時にXMLファイルにエンコードする必要のあるものだけを変換します。引用符はノード値にエンコードする必要はないため、エクスポート時にはエンコードされません。

+0

私のせいでミスしてしまいました。 [System.Security.SecurityElement] :: Escape($ var)を使用することで、私が探していたことをかなり行うことができました。 $ dbl = '"' (Get-Content $ path \ $ InFile)| Foreach-Object {$ _ -replace '(?is)(?<=。*?)"(?=。*?) [System.Security.SecurityElement] :: Escape($ dbl)} |設定内容$ path \ $ InFile これは二重引用符を取り、それを"に置き換えます - 非常に粗いですが、改善できると確信しています – zoomzoomvince

+0

実際、この例は[W3C Markup Validation Service] (https://validator.w3.org/#validate_by_input)。 – JosefZ

関連する問題