0

在 PowerShell 中,我试图在“PmtDate”标记之后立即将一个新节点插入 XML,但我不知道如何解决这个错误。

无法将参数“1”,值为:“2016-01-20”,用于“InsertAfter”以键入“System.Xml.XmlNode”:“无法转换类型为“System.String”的“2016-01-20”值" 键入 "System.Xml.XmlNode"。" 在 C:\StackOverflow.ps1:17 char:3 + $Employee.Employment.Payment.InsertAfter($LateReason, $test); + + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

简化的 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>
              <MonthNo>10</MonthNo>
            </Payment>
          </Employment>
        </Employee>
      </FullPaymentSubmission>
    </IRenvelope>
  </Body>
</GovTalkMessage>

PowerShell 脚本

$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);

}

所需的 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>
4

1 回答 1

0

所有功劳归功于“PetSerAl”的上述回应。

$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);

}
于 2016-02-04T09:23:11.347 回答