在 Powershell 中筛选 XML 数据,并以相同的 XML 格式保存到目标文件夹



我有一个XML文件,其中包含不同国家的数据。我想使用 PowerShell 加载 XML 文件,过滤数据以仅显示英国的数据,然后将结果保存到新的 XML 文件中。我可以加载数据,过滤它,但不能保存。筛选数据后,前 2 个节点(数据集和数据(在目标变量中不可见。

我尝试使用 Get-Content 加载文件,使用"$xml.dataset.data |其中 {$_.country.startsWith("UK"(}",然后使用 ".save" 保存结果。

下面是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<dataset>
    <data>
        <Name>Test1</Name>
        <Description>First test in the set</Description>
        <FilePath4>C:ApplicationApple.txt</FilePath4>
        <country>UK</country>
        <FilePath7>C:TestOrange.txt</FilePath7>
    </data>
    <data>
        <Name>Test1</Name>
        <Description>Third test in the set</Description>
        <FilePath4>C:ApplicationApple.txt</FilePath4>
        <country>ROI</country>
        <FilePath7>C:TestOrange.txt</FilePath7>
    </data>
</dataset>

数据加载: $xml = [System.Xml.XmlDocument](Get-Content C:UsersHybridPC-40DesktopSample.xml)

数据过滤器: $xml1 = $xml.dataset.data | Where {$_.country.startsWith("UK")}

结果: $xml1

Name        : Test1
Description : First test in the set
FilePath4   : C:ApplicationApple.txt
country     : UK
FilePath7   : C:TestOrange.txt

救: $xml1.save("C:UsersHybridPC-40DesktopTest.xml")给出以下错误

Method invocation failed because [System.Xml.XmlElement] does not contain a method named 'save'.
At line:1 char:1
+ $xml1.save("C:UsersHybridPC-40DesktopTest.xml")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (save:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

预期结果应是包含以下数据的新 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<dataset>
    <data>
        <Name>Test1</Name>
        <Description>First test in the set</Description>
        <FilePath4>C:ApplicationApple.txt</FilePath4>
        <country>UK</country>
        <FilePath7>C:TestOrange.txt</FilePath7>
    </data>
</dataset>
xmlelement因为

错误表明 dosent 具有 save 方法,因此包含它的xmldocument

试试这个:

$xml1.ownerdocument.save("C:UsersHybridPC-40DesktopTest.xml")

一些提示:

加载 XML

$xml = new-object System.Xml.XmlDocument
$xml.load("C:UsersHybridPC-40DesktopSample.xml")

使用 get-content 加载 XML 可能会弄乱格式。

#creates a record of all or part of a PowerShell session to a text file. 
#The transcript includes all command that the user types and all output that appears on the console.
Start-Transcript -Path "$PSScriptRootLog$($($MyInvocation.MyCommand.Name).TrimEnd("ps1"))log"

$XmlPath = "$PSScriptRootSample.xml"
# same too below ..
#$XmlPath = Join-Path -Path $PSScriptRoot -ChildPath "Sample.xml"
$SavedNewXml = "$PSScriptRootTest.xml"

$xml = [xml](Get-Content $XmlPath)
#$xml = [System.Xml.XmlDocument](Get-Content $XmlPath)
$xml1 = $xml.dataset.data | Where {$_.country.startsWith("UK")}

$xml1 | ForEach-Object {
    Write-host "Writing to $SavedNewXml  ..."
    $_ | Out-File -FilePath $SavedNewXml
}
#stops a transcript that was started by the Start-Transcript cmdlet. 
#Alternatively, you can end a session to stop a transcript.
Stop-Transcript

删除目标节点以外的数据节点并保存。

$xml.SelectNodes("//data[not(./country/@value='UK')]") | foreach { $_.ParentNode.RemoveChild($_) }
$xml.Save("your xml path")

这个怎么样...

[xml]$Country = @"
<?xml version="1.0" encoding="utf-8"?>
<dataset>
    <data>
        <Name>Test1</Name>
        <Description>First test in the set</Description>
        <FilePath4>C:ApplicationApple.txt</FilePath4>
        <country>UK</country>
        <FilePath7>C:TestOrange.txt</FilePath7>
    </data>
    <data>
        <Name>Test1</Name>
        <Description>Third test in the set</Description>
        <FilePath4>C:ApplicationApple.txt</FilePath4>
        <country>ROI</country>
        <FilePath7>C:TestOrange.txt</FilePath7>
    </data>
</dataset>
"@
$Country.dataset.data 
# Results
Name        : Test1
Description : First test in the set
FilePath4   : C:ApplicationApple.txt
country     : UK
FilePath7   : C:TestOrange.txt
Name        : Test1
Description : Third test in the set
FilePath4   : C:ApplicationApple.txt
country     : ROI
FilePath7   : C:TestOrange.txt

($NonUK = $Country.dataset.data | Where-Object {$_.Country -ne 'UK'})
# Results
Name        : Test1
Description : Third test in the set
FilePath4   : C:ApplicationApple.txt
country     : ROI
FilePath7   : C:TestOrange.txt
$Country.dataset.RemoveChild($NonUK)
# Results
Name        : Test1
Description : Third test in the set
FilePath4   : C:ApplicationApple.txt
country     : ROI
FilePath7   : C:TestOrange.txt

# Results
$Country.Save('e:tempUK_Only.xml')
Get-Content -Path 'E:tempUK_Only.xml'
# Results
<?xml version="1.0" encoding="utf-8"?>
<dataset>
  <data>
    <Name>Test1</Name>
    <Description>First test in the set</Description>
    <FilePath4>C:ApplicationApple.txt</FilePath4>
    <country>UK</country>
    <FilePath7>C:TestOrange.txt</FilePath7>
  </data>
</dataset>

最新更新