Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell将子元素添加到XML元素_Xml_Powershell_Powershell 3.0 - Fatal编程技术网

使用Powershell将子元素添加到XML元素

使用Powershell将子元素添加到XML元素,xml,powershell,powershell-3.0,Xml,Powershell,Powershell 3.0,我有一个XML文档,我正在循环浏览并下载一个文件(当它存在时)并将其保存到磁盘。 我想更新XML文件以包含保存数据的位置。我怎样才能做到最好 XML文件示例: <Invoices xmlns="http://example.com/"> <invoice> <ContractorName>TEST contractor</ContractorName> <Invoice_Number>12345</Invoic

我有一个XML文档,我正在循环浏览并下载一个文件(当它存在时)并将其保存到磁盘。 我想更新XML文件以包含保存数据的位置。我怎样才能做到最好

XML文件示例:

<Invoices xmlns="http://example.com/">
  <invoice>
    <ContractorName>TEST contractor</ContractorName>
    <Invoice_Number>12345</Invoice_Number>
    <Invoice_Date>2017-05-20</Invoice_Date>
    <Invoice_Amount>100.00</Invoice_Amount>
    <Invoice_Hyperlink>https://example.com/path/files/file.pdf</Invoice_Hyperlink>
  </invoice>
  <invoice>
    <ContractorName>TEST contractor 2</ContractorName>
    <Invoice_Number>98765</Invoice_Number>
    <Invoice_Date>2017-05-20</Invoice_Date>
    <Invoice_Amount>1000.00</Invoice_Amount>
  </invoice>
</invoices>
我找到的例子似乎都说使用
CreateElement
方法,下面的例子,但我无法让它起作用,因为上面例子中的
$r
System.Xml.XmlLinkedNode
CreateElemnt
似乎只适用于
System.Xml.XmlNode
,这将是
$Inovices
,但我希望元素位于
$Invoice.Invoices.Invoice

[xml] $doc = Get-Content($filePath)
$child = $doc.CreateElement("newElement")
$doc.DocumentElement.AppendChild($child)
如有任何建议,我将不胜感激。我希望的结果是用以下内容保存上述文件

<Invoices xmlns="http://example.com/">
  <invoice>
    <ContractorName>TEST contractor</ContractorName>
    <Invoice_Number>12345</Invoice_Number>
    <Invoice_Date>2017-05-20</Invoice_Date>
    <Invoice_Amount>100.00</Invoice_Amount>
    <Invoice_Hyperlink>https://example.com/path/files/file.pdf</Invoice_Hyperlink>
    <Local_File>D:\Invoices_Downloaded\12345_2017-05-20.pdf</Local_File>
  </invoice>
  <invoice>
    <ContractorName>TEST contractor 2</ContractorName>
    <Invoice_Number>98765</Invoice_Number>
    <Invoice_Date>2017-05-20</Invoice_Date>
    <Invoice_Amount>1000.00</Invoice_Amount>
  </invoice>
</invoices>

测试承包商
12345
2017-05-20
100
https://example.com/path/files/file.pdf
D:\Invoices\u下载\12345\u 2017-05-20.pdf
测试承办商2
98765
2017-05-20
1000

您找到的代码段工作正常。
$Invoice.Invoices.Invoice
是一个包含
XmlLinkedNodes

在foreach循环之前,将
CreateElement()
与您的
$Invoice
一起使用

$Invoicesfile = "D:\Download\invoices.xml"
[xml]$Invoice = Get-Content $Invoicesfile -Raw 

$InvoiceDestinationDir = "D:\Invoices_Downloaded\"

$child = $Invoice.CreateElement("Local_File")

foreach ($r in $Invoice.Invoices.Invoice){...}
在foreach中,您可以将
$child
附加到实际工作的节点

If($r.Invoice_Hyperlink -ne $null){
        $r.appendchild($child)
        $InvoiceDate = [datetime]::ParseExact($r.Invoice_Date, 'yyyy-MM-dd', $null)
如您所见,我从xml解析了日期,以便能够使用
$InvoiceDate.ToString()
-方法


构建
$destination
-路径后,您可以使用
$r.Local\u File=$destination

将该路径分配给新节点,谢谢。这是可行的,但当我使用$invoice.save()保存文档时,它只保存最后一个本地\u文件元素。保存修改后的图元和文件的最佳方式是什么。每次
$r.appendchild($child)
后是否需要保存我不确定应该使用的正确方法,但我所做的是创建一个新的XML对象
[XML]$newXML='
,并在循环结束时将$r导入新的XML对象
$newXML.DocumentElement.appendchild($newXML.ImportNode($r,$true))|输出Null
然后保存文件
$newXML.save($Invoicesfile)
If($r.Invoice_Hyperlink -ne $null){
        $r.appendchild($child)
        $InvoiceDate = [datetime]::ParseExact($r.Invoice_Date, 'yyyy-MM-dd', $null)