1

I'm using below code from this answer to get value from id and I need to replace value of id in csv from xml by and save csv files :

 $CSVpath = "G:\1.csv"
    $xmlPath = "G:\1.xml"
    
    # This is a more robust (and faster) way to load XML files.
    ($xmlDoc = [xml]::new()).Load((Convert-Path $xmlPath))
    
    # Import the CSV.
    $csvRows = Import-Csv $CSVpath
    # Get the column names.
    $columnNames = $csvRows[0].psobject.Properties.Name
    
    foreach ($csvRow in $csvRows) {
      foreach ($columnName in $columnNames) {
        $id = $csvRow.$columnName
        if (-not $id) { continue }
        @($xmlDoc.enum_types.enum_type.Where({ $_.field_name -eq $columnName }, 'First').
          items).ForEach({ $_.item }).Where({ $_.id -eq $id }).value
      }
    }

XML:

<enum_types>
    
        <enum_type field_name="Test1">
            <items>
                <item>
                    <id>1</id>
                    <value>A</value>
                </item>
            </items>
            </enum_type>
<enum_type field_name="Test2">
            <items>
                <item>
                    <id>1</id>
                    <value>A</value>
                </item>
            </items>
            </enum_type>
        </enum_types>

Please find below CSV Files for replace value ID value from above xml: https://i.stack.imgur.com/siXys.png

mklement0
  • 382,024
  • 64
  • 607
  • 775
microset
  • 276
  • 1
  • 2
  • 12
  • To me this is not very clear... You just show a small portion of the XML and an image of the CSV. Please open the CSV in Notepad, copy the first 3 or 4 lines and paste these in in your question as formatted text. Now it is unclear why there are empty fields/rows in there. – Theo Aug 26 '21 at 10:23

1 Answers1

1

You need to modify the foreach loop to update the (in-memory) CSV rows (objects), and then save them back to a CSV file:

# ... 

foreach ($csvRow in $csvRows) {
  foreach ($columnName in $columnNames) {
    $id = $csvRow.$columnName
    if (-not $id) { continue }
    $newId = @($xmlDoc.enum_types.enum_type.Where( { $_.field_name -eq $columnName }, 'First').
               items).ForEach( { $_.item }).Where( { $_.id -eq $id }).value
    # Update the relevant CSV field.
    # Note: If the ID wasn't found, `$newId` contains $null
    #       and the field will be blanked out.
    $csvRow.$columnName = $newId
  }
}

# Export the updated CSV rows (objects)
# Note: This writes back to the input CSV file.
#       Be sure to make a backup of the original first.
# Adjust -Encoding as needed.
$csvRows | Export-Csv -NoTypeInformation -Encoding utf8 $CSVpath
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thanks Man for your Unforgettable help! Really Really Appreciated Sir. Sir, If I want learn advance powershell then how to start learn , If you have any source or any document then please share. – microset Aug 26 '21 at 18:23
  • 2
    Glad to hear I was of help; my pleasure. Re learning resources: a while back I compiled a few in [this answer](https://stackoverflow.com/a/48491292/45375) - I hope they're still relevant and helpful. – mklement0 Aug 26 '21 at 18:52
  • mklement0 , Sir script is taking too much time due to 11000 row , is there any possibility to reduce time or we can use 'Foreach-Object'. – microset Sep 12 '21 at 14:16