Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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中获取子节点列表_Powershell - Fatal编程技术网

Powershell 如何从XML中获取子节点列表

Powershell 如何从XML中获取子节点列表,powershell,Powershell,给定以下示例脚本(在我的硬盘上另存为sample.txt): 有没有更好的方法来填充*$found_fields*而不是创建一个CSV文件,将第一行存储在变量中,然后删除CSV文件?尝试将-展开从节点更改为名称(以及其中的对象以排除标题和价格) 这将为您提供book的所有意外子节点完美!不仅回答了我的问题,而且帮助我看到了我的原始脚本中的一个弱点:“新”图书在原始节点未显示后添加的子节点。这行代码修复了这个问题。泰! DECLARE @xVar XML SET @xVar = '<b

给定以下示例脚本(在我的硬盘上另存为sample.txt):


有没有更好的方法来填充*$found_fields*而不是创建一个CSV文件,将第一行存储在变量中,然后删除CSV文件?

尝试将
-展开
节点更改为名称(以及
其中的对象
以排除标题和价格)


这将为您提供
book

的所有意外子节点完美!不仅回答了我的问题,而且帮助我看到了我的原始脚本中的一个弱点:“新”图书在原始节点未显示后添加的子节点。这行代码修复了这个问题。泰!
DECLARE @xVar XML
SET @xVar = 
  '<bookstore>
      <book>
        <title>Writing Secure Code</title>
        <author>
          <first-name>Michael</first-name>
          <last-name>Howard</last-name>
        </author>
        <author>
          <first-name>David</first-name>
          <last-name>LeBlanc</last-name>
        </author>
        <price>39.99</price>
      </book>
      <book>
        <title>Old Man and the sea</title>
        <author>
          <first-name>Earnest</first-name>
          <last-name>Hemmingway</last-name>
        </author>
        <price> 9.99</price>
      </book>
    </bookstore>
    '

SELECT nref.value('first-name[1]', 'nvarchar(50)') FirstName,
       nref.value('last-name[1]', 'nvarchar(50)') LastName
FROM   @xVar.nodes('//author') AS R(nref)
WHERE  nref.exist('.[first-name != "David"]') = 1
Set-StrictMode -Version Latest

cls
#The list of fields that I want to find
[array]$expected_fields = "title", "price"

#extract the xml from sample.txt and put it in an xml variable
[string]$file_script = Get-Content "C:\PowerShellScripts\sample.txt"
[int]$start_pos = $file_script.IndexOf("'") + 1
[int]$end_pos = $file_script.SubString($start_pos + 1).IndexOf("'") + 1
[xml]$xml_result = $file_script.SubString($start_pos,$end_pos)

#NOTE:  THIS IS THE PART THAT FEELS WRONG
#Convert the xml snipput into CSV file and then get the headers (which is the only thing I want)
$export_file_name = "C:\PowerShellScripts\test.csv"
Select-Xml 'child::bookstore/book' $xml_result  | Select-Object -expand Node | Export-Csv $export_file_name -NoTypeInformation -Delimiter:"`t" -Encoding:UTF8 
[string]$field_names = Get-Content $export_file_name | Select-Object -first 1
Remove-Item "C:\Users\Jennifer\Google Drive\PowerShellScripts\test.csv"
[array]$found_fields = $field_names.Replace("""","").Split("`t")

#report new fields
foreach ($specific_field in $found_fields) {
    if ($expected_fields -notcontains $specific_field)
    {
        Write-Host "New field found:" $specific_field
    }
}
$xml_result.SelectNodes("bookstore/book/*") | Select-Object -Expand Name | Where-Object { ($_ -ne "title") -and ($_ -ne "price") }