Replace text in .xml file when find matching value in one field but need to update data in another field

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
Locked
User avatar
timstspry11
Posts: 11
Joined: Tue Jul 21, 2015 12:42 pm

Replace text in .xml file when find matching value in one field but need to update data in another field

Post by timstspry11 » Mon Dec 21, 2015 10:31 am

Hi, I have spent much time trying to figure out how to do this. I will do my best to explain what I am trying to do. Below is a single row of data from the .xml file I am trying to update:

<Row AreaParentID="2" ChildAreaID="2" ParentAreaID="1" StartDate="1986-06-01T00:00:00" CreateDate="2013-10-17T14:28:01.24" ChangeDate="2013-10-17T14:28:01.24" CreateWorkstationID="12345" ChangeWorkstationID="12345"/>

There will only ever be one row in the file that will match what I am looking for. What I need to do is find a given AreaParentID and then either update the StartDate or remove the start date from the row that was found. I have seen all sorts of examples using creplace and even RegEx, but they have all been fairly simplistic updating the same field that is being searched. Oh, I am not good with regular expressions and have never really used one in PowerShell.

Thanks in advance for any help provided!

Tim

User avatar
jvierra
Posts: 11559
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Replace text in .xml file when find matching value in one field but need to update data in another field

Post by jvierra » Mon Dec 21, 2015 10:42 am

  1. [xml]$xml=@'
  2.  
  3. <root>
  4.  
  5. <Row
  6.  
  7.     AreaParentID="2"
  8.  
  9.     ChildAreaID="2"
  10.  
  11.     ParentAreaID="1"
  12.  
  13.     StartDate="1986-06-01T00:00:00"
  14.  
  15.     CreateDate="2013-10-17T14:28:01.24"
  16.  
  17.     ChangeDate="2013-10-17T14:28:01.24"
  18.  
  19.     CreateWorkstationID="12345"
  20.  
  21.     ChangeWorkstationID="12345"
  22.  
  23. />
  24.  
  25. </root>
  26.  
  27. '@
  28.  
  29.  
  30.  
  31. $n=$xml.SelectSingleNode('//Row[@AreaParentID="2"]')
  32.  
  33. $n.StartDate
  34.  
  35. #1986-06-01T00:00:00
  36.  
  37. $n.StartDate='2015-12-21T00:00:00'
  38.  
  39. $n.StartDate
  40.  
  41. #2015-12-21T00:00:00
  42.  
  43. $xml.Save($filename)

User avatar
timstspry11
Posts: 11
Joined: Tue Jul 21, 2015 12:42 pm

Re: Replace text in .xml file when find matching value in one field but need to update data in another field

Post by timstspry11 » Mon Dec 21, 2015 2:10 pm

Thank you for your help! I do appreciate it.

Tim

User avatar
jvierra
Posts: 11559
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Replace text in .xml file when find matching value in one field but need to update data in another field

Post by jvierra » Mon Dec 21, 2015 2:31 pm

That will not work if the XML is wrapped in custom namespaces. If so you will have to add more steps.

Locked