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

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
timstspry11
Posts: 15
Last visit: Wed Sep 18, 2019 6:41 am

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

Post by timstspry11 »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

  1. [xml]$xml=@'
  2. <root>
  3. <Row
  4.     AreaParentID="2"
  5.     ChildAreaID="2"
  6.     ParentAreaID="1"
  7.     StartDate="1986-06-01T00:00:00"
  8.     CreateDate="2013-10-17T14:28:01.24"
  9.     ChangeDate="2013-10-17T14:28:01.24"
  10.     CreateWorkstationID="12345"
  11.     ChangeWorkstationID="12345"
  12. />
  13. </root>
  14. '@
  15.  
  16. $n=$xml.SelectSingleNode('//Row[@AreaParentID="2"]')
  17. $n.StartDate
  18. #1986-06-01T00:00:00
  19. $n.StartDate='2015-12-21T00:00:00'
  20. $n.StartDate
  21. #2015-12-21T00:00:00
  22. $xml.Save($filename)
User avatar
timstspry11
Posts: 15
Last visit: Wed Sep 18, 2019 6:41 am

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

Post by timstspry11 »

Thank you for your help! I do appreciate it.

Tim
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

That will not work if the XML is wrapped in custom namespaces. If so you will have to add more steps.
This topic is 8 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked