从xml文件中提取值
我有一个.yml文件,我必须从中提取数据 我使用下面的代码将文件转换为xml,但现在不知道如何从中提取数据 我考虑过使用selectxml来转换为xml从xml文件中提取值,xml,powershell,Xml,Powershell,我有一个.yml文件,我必须从中提取数据 我使用下面的代码将文件转换为xml,但现在不知道如何从中提取数据 我考虑过使用selectxml来转换为xml $Xml = "D:\DIR_LIST.yml" $Cont=@() $Cont = Get-Content -Path $Xml $Cont | ConvertTo-Xml -As String | Out-File "D:\newxml.xml" 下面是xml数据 <?xml version="1.0" encoding="utf
$Xml = "D:\DIR_LIST.yml"
$Cont=@()
$Cont = Get-Content -Path $Xml
$Cont | ConvertTo-Xml -As String | Out-File "D:\newxml.xml"
下面是xml数据
<?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object Type="System.String"><?xml version="1.0" encoding="UTF-8"?></Object>
<Object Type="System.String"><tree></Object>
<Object Type="System.String"> <directory name="."></Object>
<Object Type="System.String"> <file name="DIR_LIST.txt" size="14"></file></Object>
<Object Type="System.String"> <file name="DIR_LIST.yml" size="0"></file></Object>
<Object Type="System.String"> <file name="File_Details.sh" size="252"></file></Object>
<Object Type="System.String"> <directory name="test1" size="4096"></Object>
<Object Type="System.String"> <directory name="sub1" size="4096"></Object>
<Object Type="System.String"> <directory name="sub2" size="4096"></Object>
<Object Type="System.String"> </directory></Object>
<Object Type="System.String"> </directory></Object>
<Object Type="System.String"> </directory></Object>
<Object Type="System.String"> <directory name="test2" size="4096"></Object>
<Object Type="System.String"> </directory></Object>
<Object Type="System.String"> </directory></Object>
<Object Type="System.String"> <report></Object>
<Object Type="System.String"> <directories>4</directories></Object>
<Object Type="System.String"> <files>3</files></Object>
<Object Type="System.String"> </report></Object>
<Object Type="System.String"></tree></Object>
</Objects>
?xml version=“1.0”encoding=“UTF-8”?
树
目录名=”
file name=“DIR\u LIST.txt”size=“14”/file
file name=“DIR\u LIST.yml”size=“0”/file
file name=“file\u Details.sh”size=“252”/file
目录名=“test1”size=“4096”
目录名=“sub1”size=“4096”
目录名=“sub2”size=“4096”
/目录
/目录
/目录
目录名=“test2”size=“4096”
/目录
/目录
报告
目录4/目录
文件3/文件
/报告
/树
要提取目录名=值吗
请让我知道如何继续,或者我所做的方法有误。文件未正确转换,但是您可以使用下面的脚本,然后进行一些文本修改
$xmlpath = 'path\to\xml'
[xml]$xmldoc = Get-Content -Path $xmlpath
$objects = $xmldoc.Objects.Object
$objects | Select-Object -Property '#text' | where {$_ -match 'directory name'}
[1] 为什么使用
-As字符串
而不是默认的-As文档
?[2] 您是否尝试过使用-NoTypeInformation
参数?