CantGetItToWork
CantGetItToWork

Reputation: 25

Powershell XML InsertAfter Error

In PowerShell I'm trying to insert a new node into the XML immediately after the "PmtDate" tag but I can't work out how to get around this error.

Cannot convert argument "1", with value: "2016-01-20", for "InsertAfter" to type "System.Xml.XmlNode": "Cannot convert the "2016-01-20" value of type "System.String" to type "System.Xml.XmlNode"." At C:\StackOverflow.ps1:17 char:3 + $Employee.Employment.Payment.InsertAfter($LateReason, $test); + + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Simplified XML File

<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
  <EnvelopeVersion>2.0</EnvelopeVersion>
  <Body>
    <IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/15-16/1">
      <FullPaymentSubmission>
        <Employee>
          <EmployeeDetails>
            <Name>
              <Ttl>MR</Ttl>
              <Fore>BURT</Fore>
              <Sur>BURT</Sur>
            </Name>
          </EmployeeDetails>
          <Employment>
            <PayId>11111</PayId>
            <Payment>
              <PayFreq>M1</PayFreq>
              <PmtDate>2016-01-20</PmtDate>
              <MonthNo>10</MonthNo>
            </Payment>
          </Employment>
        </Employee>
      </FullPaymentSubmission>
    </IRenvelope>
  </Body>
</GovTalkMessage>

PowerShell Script

$Path = "\\c03fps01v\StackOverflow.xml" 
$FPS=[xml](Get-Content $Path); 

# Loops through each employee in the FPS File
foreach( $Employee in $FPS.GovTalkMessage.Body.IREnvelope.FullPaymentSubmission.Employee) 
{ 

# Creates new XML element, sets value to H and then appends at the parent level
  $LateReason=$FPS.CreateElement("LateReason","http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/15-16/1")
  $LateReason.set_InnerText("H")
  $test=$Employee.Employment.Payment.PmtDate;
  $Employee.Employment.Payment.InsertAfter($LateReason, $test);

}

Desired XML

<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
  <EnvelopeVersion>2.0</EnvelopeVersion>
  <Body>
    <IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/15-16/1">
      <FullPaymentSubmission>
        <Employee>
          <EmployeeDetails>
            <Name>
              <Ttl>MR</Ttl>
              <Fore>BURT</Fore>
              <Sur>BURT</Sur>
            </Name>
          </EmployeeDetails>
          <Employment>
            <PayId>11111</PayId>
            <Payment>
              <PayFreq>M1</PayFreq>
              <PmtDate>2016-01-20</PmtDate>
              <LateReason>H</LateReason>
              <MonthNo>10</MonthNo>
            </Payment>
          </Employment>
        </Employee>
      </FullPaymentSubmission>
    </IRenvelope>
  </Body>
</GovTalkMessage>

Upvotes: 0

Views: 559

Answers (1)

CantGetItToWork
CantGetItToWork

Reputation: 25

All credit to "PetSerAl" for his response above.

$Path = "\\c03fps01v\StackOverflow.xml" 
$FPS=[xml](Get-Content $Path); 

# Loops through each employee in the FPS File
foreach( $Employee in $FPS.GovTalkMessage.Body.IREnvelope.FullPaymentSubmission.Employee) 
{ 

# Creates new XML element, sets value to H and then appends at the parent level
  $LateReason=$FPS.CreateElement("LateReason","http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/15-16/1")
  $LateReason.set_InnerText("H")
  $test=$Employee.Employment.Payment.Item('PmtDate');
  $Employee.Employment.Payment.InsertAfter($LateReason, $test);

}

Upvotes: 0

Related Questions