2017-05-20 15 views
1

ノードテキスト内でいくつかの置換操作を行うために必要な一連のXMLファイルがあります。私のコードは見つけることができないので<Notes>ノードを更新するように見えません。私はメモ帳のXMLパスをチェックした+ +とそれは正しいようです。XMLノードで作業しない置換

これは、テストに使用しているサンプルXMLファイルです。

<?xml version="1.0" encoding="UTF-8"?> 
<OrganisationUnits> 
    <OrganisationUnitsRow num="21"> 
    <OrganisationId>ORG1</OrganisationId> 
    <OrganisationName>ORG 1 TEST</OrganisationName> 
    <Addresses> 
     <AddressesRow num="1"> 
     <AddressId>E41002</AddressId> 
     <MainAddress>Y</MainAddress> 
     </AddressesRow> 
    </Addresses> 
    <ContactDetails> 
     <ContactDetailsRow num="1"> 
     <ContactId>E6366</ContactId> 
     <ContactCode>OFFICE</ContactCode> 
     <Main>N</Main> 
     <Notes>start date 11/03/1994</Notes> 
     </ContactDetailsRow> 
     <ContactDetailsRow num="2"> 
     <ContactId>E6367</ContactId> 
     <ContactCode>WORK</ContactCode> 
     <Main>N</Main> 
     <Notes>start date 11/03/1995</Notes> 
     </ContactDetailsRow> 
     <ContactDetailsRow num="3"> 
     <ContactId>E6368</ContactId> 
     <ContactCode>HOME</ContactCode> 
     <Main>Y</Main> 
     <Notes>start date - after 11/03/1995</Notes> 
     </ContactDetailsRow> 
     <ContactDetailsRow num="4"> 
     <ContactId>E6369</ContactId> 
     <ContactCode>EMAIL</ContactCode> 
     <ContactCodeDesc>Email Address</ContactCodeDesc> 
     <ContactValue>[email protected]</ContactValue> 
     <StartDate>2000-03-11</StartDate> 
     <Main>N</Main> 
     <Notes>more test notes</Notes> 
     </ContactDetailsRow> 
    </ContactDetails> 
    <Sector>P</Sector> 
    <SectorDesc>Private</SectorDesc> 
    </OrganisationUnitsRow> 
</OrganisationUnits> 

これは私のスクリプトです。

#----- get source xml file(e) from the folder below ---# 
$path = "C:\Dump\TEST" 
$Files = Get-Childitem -path $path -File -include test_file_1.xml -name 

# For every file in the folder specified perform a series of actions below 
foreach ($File in $Files) 
{ 
    Write-Host "=== FILE is $File ===" -ForegroundColor "White" 
    $xml = [xml](Get-Content $path\$File) 

    ## SectorDesc 
    # check to ensure that we have got a SectorDesc row 
    foreach ($OrganisationUnitsRow in $xml.OrganisationUnits.OrganisationUnitsRow) 
    { 
     if ($OrganisationUnitsRow.Item('SectorDesc')) 
     { 
      $xml.OrganisationUnits.OrganisationUnitsRow.SectorDesc = $xml.OrganisationUnits.OrganisationUnitsRow.SectorDesc.Replace("`b" , "\b")    #backspace 
      $xml.OrganisationUnits.OrganisationUnitsRow.SectorDesc = $xml.OrganisationUnits.OrganisationUnitsRow.SectorDesc.Replace("`f" , "\f")    #form feed 
     } 
    } 

    ## Notes 
    # check to ensure that we have got a Notes row 
    foreach ($ContactDetailsRow in $xml.OrganisationUnits.OrganisationUnitsRow.ContactDetails.ContactDetailsRow) 
    { 
     if ($ContactDetailsRow.Item('Notes')) 
     { 
      Write-Host "$ContactDetailsRow.Notes" 
      $xml.OrganisationUnits.OrganisationUnitsRow.ContactDetails.ContactDetailsRow.Notes = $xml.OrganisationUnits.OrganisationUnitsRow.ContactDetails.ContactDetailsRow.Notes.Replace("\" , "\\")    #back slash 
      $xml.OrganisationUnits.OrganisationUnitsRow.ContactDetails.ContactDetailsRow.Notes = $xml.OrganisationUnits.OrganisationUnitsRow.ContactDetails.ContactDetailsRow.Notes.Replace("`b" , "\b")    #backspace 
     } 

    } 

    $xml.Save("$path\$File") 
} 

スクリプトを実行すると、エラーが発生します。

 
Property 'Notes' cannot be found on this object; make sure it exists and is 
settable. 
At C:\Dump\test10.ps1:33 char:8 
+     $xml.OrganisationUnits.OrganisationUnitsRow.ContactDetails.ContactDetails ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : PropertyAssignmentException 

Property 'Notes' cannot be found on this object; make sure it exists and is 
settable. 
At C:\Dump\test10.ps1:34 char:8 
+     $xml.OrganisationUnits.OrganisationUnitsRow.ContactDetails.ContactDetails ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : PropertyAssignmentException 

私はむしろ明白かもしれませんが、私はそれをすぐに見ることはできません。

答えて

3

表現$xml.Organis...ailsRow.Notesはあなた(あなたの例4で)すべて<ContactDetailsRow>ノードの<Notes>子ノードを示します。

foreach ($ContactDetailsRow in $xml.OrganisationUnits.OrganisationUnitsRow.ContactDetails.ContactDetailsRow) 
{ 
    if ($ContactDetailsRow.Item('Notes')) 
    {   
     Write-Host "$ContactDetailsRow.Notes" 
     $ContactDetailsRow.Notes = $ContactDetailsRow.Notes.Replace("\", "\\").Replace("`b", "\b") 
    } 
} 

いっそのこと、直接XPath式で<Notes>子ノードを選択します:

$xml.SelectNodes('//ContactDetailsRow/Notes') | ForEach-Object { 
    $_.'#text' = $_.'#text'.Replace("\", "\\").Replace("`b", "\b") 
} 

理由を代わりにループ変数を使用します(そして、あなたはデイジーチェーン接続することができますReplace()通話)あなたのコードは最初の入れ子になったループ(同じ間違いを犯した場所)で同じエラーをスローしません。これは、XMLサンプルにノードが1つだけ含まれているためです。

+0

なぜ私のコードが間違っているのかを説明する@Ansgar Wiechersのおかげで - それが間違っていた理由がある – zoomzoomvince

関連する問題