2

I got 2 XML files I'm reading a value from the first and need to replace that value in the second XML in a specific string, this is what I got so far and it doesn't work well it change the whole XML to #document for some reason

EDIT Reworking my whole logic for the script and I'm stuck with trimming the node, getting an error at IdexOf why?

<BunnyTemplate> <Parm Name="A-1" Source="Application" OnAbsence="1.com"> <Parm Name="A-2" Source="Application" OnAbsence="2.com"> <Parm Name="A-3" Source="Application" OnAbsence="\\3\3"/> <Parm Name="A-4" Source="Application" OnAbsence="4.com"> </BunnyTemplate> 
$file1 = "C:\..\Desktop\file1.xml"
$file1Content = [xml](Get-Content $file1)
$targetNode = $file1Content.SelectSingleNode("//General/@findMe")
$trimmedNode = $targetNode.Substring(0, $targetNode.IndexOf('last'))
8
  • Please show sample xml content of $file2 Commented May 11, 2020 at 15:25
  • @MathiasR.Jessen I can't it work related, <something> </something> looks like that with values inside Commented May 11, 2020 at 15:33
  • 1
    Why not add a sample with dummy values and/or names then? Commented May 11, 2020 at 15:34
  • <BunnyTemplate> <Parm Name="Bunny" Source="Bunny" OnAbsence="value-to-replace"/> </BunnyTemplate> @Remko hope this helps Commented May 11, 2020 at 15:37
  • In the sample XML you can simply do: $file1.BunnyTemplate.parm.OnAbsence = 'NewValue' Commented May 11, 2020 at 15:42

1 Answer 1

5

Don't use -replace on an XML document, use XPath instead.

Assuming $file2 contains:

<BunnyTemplate>
  <Parm Name="Bunny" Source="Bunny" OnAbsence="value-to-replace"/>
</BunnyTemplate>

... you could do the following:

$file2Xml = [xml](Get-Content $file2)

# Locate target node
$targetNode = $file2Xml.SelectSingleNode("//Parm[@OnAbsence]")

# Overwrite attribute value
$targetNode.SetAttribute('OnAbsence', "text${trimmedNode}")

# save file
$file2Xml.Save("C:\path\to\output.xml")

SelectSingleNode("//Parm[@OnAbsence]") will return the first <Parm> node that has an OnAbsence attribute.

If you specifically need to target one with Source="Bunny", you could use //Parm[@Source = 'Bunny'] instead

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the quick replay, I'm not sure how I should use the xpath could you provide an example?
@BugsBunny You're already using XPath to retrieve the value from $file1! If you update your question with a sample of $file2 I can show you, just redact any company-specific values
@BugsBunny Updated the answer
Hmm idk what went wrong but now if I try to Get-Content from the XML file and write-host it I don't get anything but no error
@BugsBunny please stop posting code in comments. Edit your post and add a sample for both files
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.