Changing text inside and existing XML file

Welcome Forums General PowerShell Q&A Changing text inside and existing XML file

Viewing 16 reply threads
  • Author
    Posts
    • #263067
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      I have an xml file which makes reference to several other files listed one after the other in order. The files listed have some basic metadata listed for each of these files respectively. The metadata includes such entries as File Size, MD5 hash, and File Type.

      Can someone provide me with some sample code that shows how to go in and change these metadata values?

      Specifically I can Generate a new MD5 Hash and I can determine file size with Powershell scripting but then how do I take that MD5 hash and File Size result and use them to replace old existing MD5, File Size, and File Type information listed for multiple file listings within the XML document?

      I was thinking of something like assigning a variable to each on of the results that were generated by PowerShell but then I still have no way to reference those variables to the various files and their respective metadata uniquely, that will be listed in the XML document. I need to be able to perform all of the above-mentioned automatically.

      Please answer ASAP.

      Thank you

      • This topic was modified 1 week, 6 days ago by nal2us224.
    • #263178
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      Didn’t the last thread in below post help ?

      https://powershell.org/forums/topic/return-keystroke-in-powershell/

    • #263265
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      Last thread is VERY HELPFUL for changing text in one node. i.e. <size>”Data Needing to be Changed”</size>.

      But what if that same XML file has multiple nodes with the same name? How can you tell PowerShell which node to apply the change to?

      i.e. <size>”Data Needing to be Changed”</size>

      further down in the script another instance of same thing:
      <size>”Data Needing to be Changed”</size>

      further down in the script same thing… and so on.

      How can powershell tell the difference so it knows which Node to place the data into?

    • #263292
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      Will give some tips.

      Search for Using Xpaths in PowerShell. XML stuffs via PowerShell is a vast thing. But this will help you to get started.

    • #263322
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      ok thank you

    • #263355
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      For Xpaths in PowerShell research I can find many examples and uses for this tool for a specific NODE. Doesn’t seem to work when you have multiple nodes with the same name within one XML document.

      Also the above mentioned tool has a primary use of Query inside XML document, not updating data with in a Node.

      Your thoughts Mr. Kvprasoon?

      • This reply was modified 1 week, 5 days ago by nal2us224.
    • #263427
      Participant
      Topics: 7
      Replies: 137
      Points: 668
      Helping Hand
      Rank: Major Contributor

      If the ParentNode is not the same, you can trigger off that.

    • #263630
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      Tony:

      Any scripting examples you would like to share based on your initial response please?

    • #263633
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      @nal2us2 There are filters in XML, you can use it with XPath. It acts like a where condition. Better if you can share a sample XML of what you have. use gist.github.com to share XML.

    • #263657
      Participant
      Topics: 7
      Replies: 137
      Points: 668
      Helping Hand
      Rank: Major Contributor

      Not knowing the structure of your XML, and what you ask having many different answers, as Kvprasoon has suggested, can you share what you have so far?

      You state there will be many different <size> attributes. Each one of those SHOULD have a ParentNode. If that Node is different for each of the Child <size> nodes, my thought was to use the ParentNode to decide which <size> attribute to change.

      $xmlData = New-Object System.XML.XMLDocument
      $xmlData.PreserveWhiteSpace = $True
      $xmlDoc = 'C:\Path\To\XML\file.xml'
      $xmlData.Load($xmlDoc)
      $xmlSizeNodes = $xmlData.SelectNodes('/*/*/*/Size')

      The filter shown is an example only not knowing your XML Structure

      You can then do something like:

      ForEach($node in $xmlSizeNodes) {

      if ($node.ParentNode.Name -Match 'WhatEver') {

      do something

      }

      Writing the XML file back is pretty straightforward:

      $xmlData.Save($xmlDoc)

    • #263960
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      OK Tony, here is a sample of the XML I wish to change.
      Notice all the similar tag names:

      You will notice all the similar tag names. Can you provide some sample code of how to choose which tag gets the new data that I wish to input?

    • #264104
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      XML tags can have same names, but they will be under different parents and there will be some unique value to differentiate them, in this case I can see filename is unique. If you wan to get checksum for a filename

    • #264146
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      This is the exact problem.   The unique value is the data INSIDE the tags.    I need to be able to differentiate one tag of the same name from another in code.   The fact that one tag may be the parent tag…. great!  I am assuming that will be the first tag listed.   The problem is the XML document will change from time to time the number of  “Same Name” tags it has in it, and the data will be different every time.  In the example above that I have listing three video files, the next XML sheet to be used will: 1.  have a different XML name altogether, 2. Have a different number of video files and associated “Same Named” tags, AND will have different data that needs to be updated in one, some, or all of the same named tags.   So again, some sample code of showing how to overcome these multiple issues in powershell would be appreciated.   I was thinking of something like regex expressions, but need some sample code showing how to overcome these multiple obstacles kvprasoon, or anyone else out there who can give me something other than a “general answer”.  Thank you and God bless you!!!

    • #264179
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      you can filter XML with any unique value, either the value is an attribute value(<foo attrubute-‘value’ />) or inner text (<foo>some value</foo>)

      For anything changes dynamically, you can take those as user input via parameters. If you can share some code you have so far, it will be easy to understand where you are getting issue.

    • #264182
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      I have no code at this time.  Only that XML example which I shared earlier.

       

      Shukriya

       

    • #264200
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      You have to start putting up some code, otherwise it will be difficult for you and anyone here trying to help you out. If you have some code it will help us correcting the understanding between us.

    • #264788
      Participant
      Topics: 7
      Replies: 137
      Points: 668
      Helping Hand
      Rank: Major Contributor

      The way your XML file is structured, you will simply have to choose what attribute you want to use as the unique value. As kvprasoon has suggested, the filename attribute is a likely one to choose and his code looks like what you need. You wont be able to use ParentNode as it is not unique in your case.

Viewing 16 reply threads
  • You must be logged in to reply to this topic.