I have this unusual XML data structure, where I need to update the Job Number attribute value.
<?xml version="1.0"?>
<MiscContainer version="2018-1">
<Object name="MainObject" type="TDM_Container">
<Object name="OrderList" type="TDM_List_Order">
<List name="Items">
<Object type="TDM_Item_Order">
<Property name="TraySystemType" value="stNone"/>
</Object>
</List>
</Object>
<Object name="CustomDataList" type="TDM_List_CustomData">
<List name="Items">
<Object type="TDM_Item_CustomData">
<Property name="CustomDataID" value="CDAB5D3000B66D45D69F4F27074E55E0D8"/>
<Property name="FieldCaption" value="ZB Encode Account#"/>
<Property name="Value" value="New"/>
<Property name="Kind" value="cdkPublic"/>
</Object>
<Object type="TDM_Item_CustomData">
<Property name="CustomDataID" value="CDB0C755BF8BEB45419DED0B8D4A5CF8B4"/>
<Property name="FieldCaption" value="PO#"/>
<Property name="Value" value="000009"/>
<Property name="Kind" value="cdkPublic"/>
</Object>
<Object type="TDM_Item_CustomData">
<Property name="CustomDataID" value="CDB10DBE4FD0BD4CAA96B22AA6CD9A83C2"/>
<Property name="FieldCaption" value="Job Number"/>
<Property name="Value" value="1234"/>
<Property name="Kind" value="cdkPublic"/>
</Object>
<Object type="TDM_Item_CustomData">
<Property name="CustomDataID" value="CDD69CA4A79BE145369C3FD889E18E0BCF"/>
<Property name="FieldCaption" value="coupon 1"/>
<Property name="Value" value="3123213"/>
<Property name="Kind" value="cdkPublic"/>
</Object>
I couldn't figure it out using an XML object, so I tried to use just the string replace function. The problem with the replace function is I need to search across two lines to find the unique match. I used a regex expression (.*
) to handle the possibility of there not being a value there at all, but I need to insert a value.
Here is my code:
$Folder = '\\3ipbg-fs-p01\private\UDI-Data-3i-Dev\inbound\archive\'
Get-ChildItem $Folder | ForEach-Object -Process {
if ($_.PSIsContainer) {
$sFolderPath = $_.FullName
$sFolderName = Split-Path $sFolderPath -Leaf
Get-ChildItem $sFolderPath | Where {
$_.Name -like $sFolderName + '.xml'
} | foreach {
$sFilepath = $_.FullName
(Get-Content $sFilepath) -replace '<Property name="FieldCaption" value="Job Number"/>
<Property name="Value" value="(.*)"/>','<Property name="FieldCaption" value="Job Number"/>
<Property name="Value" value="1234"/>' | Out-File -Encoding Ascii $sFilepath
}
}
}
But it doesn't work, I think because of the CR and LF. Just for testing purposed, if I remove the multi-line, it works, but I need the multi-line because single line is not unique.
Hoping someone can help either with the string replace or the XML route.