gpt4 book ai didi

xml - 为什么这个 Powershell 函数有时返回一个列表而其他时候返回一个 XmlElement?

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:30 25 4
gpt4 key购买 nike

出于某种原因,如果我的 XML 文件只包含一个计算机节点,下面的 Read-TestControllerXml powershell 函数将返回一个 System.Xml.XmlElement 类型的对象,但如果有,它会返回一个列表(这是我一直期望的)是多个计算机节点。

XML文件示例如下:

<test_controller>
<defaults>
<nunit path='C:\Program Files (x86)\NUnit 2.6.2\bin\nunit-console.exe' />
<nunit_results local_path='C:\Test_Results' remote_path='C:\Test_Results' />
<powershell path='C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe' />
<psexec path='C:\PSTools\PsExec.exe' />
</defaults>

<platform os='windows' version='2012' cpu='x64'>
<computer hostname='VM-2012R2-03' alias='standalone-01' configuration='standalone' image='VM-2012R2-03:Clean' />
</platform>
</test_controller>

这里是 powershell 函数:

####################################################################
# Parses the TestController.xml file and returns a list of Computer XML objects.
#
# Syntax:
# Read-TestControllerXml [-TestControllerXml] <path to TestController.xml> [<CommonParameters>]
#
# Returns: An array of [Xml] nodes for each of the <computer> tags in TestController.xml.
####################################################################
function Read-TestControllerXml
{
[CmdletBinding()]
Param
(
[parameter(Mandatory=$true)]
[string] $TestControllerXml
)

$ErrorActionPreference = "Stop"

# Read the TestController XML configuration file
$TestController = [xml] (Get-Content $TestControllerXml)

Write-Verbose "Selecting computer nodes..."
$computerNodes = $TestController.SelectNodes("//test_controller/platform/computer")
Write-Verbose "Selecting default nodes..."
$defaultNodes = $TestController.SelectSingleNode("//test_controller/defaults")

$computers = @()

foreach ($computerNode in $computerNodes)
{
Write-Verbose "***** Before adding default attributes *****"
Write-Verbose "$($computerNode.OuterXml)"

# Add default attributes to the node
foreach ($dnode in $defaultNodes.ChildNodes)
{
if ($dnode.NodeType -eq "Comment") { continue } # Skip comments.

# Search for the node items in the defaults section
$cnode = $computerNode

if ($dnode.Name -ne $computerNode.Name)
{
Write-Verbose "Selecting '$($dnode.Name)' nodes..."
$cnode = $computerNode.SelectSingleNode($dnode.Name)
}

# Append the default nodes that do not exist in the computer specific sections
if ($cnode -eq $null)
{
Write-Verbose "*** cnode != null cnode.OuterXml is: '$($cnode.OuterXml)'"
Write-Verbose "Appending default '$($dnode.Name)' node that don't exist in the computer nodes..."
$cnode=$computerNode.AppendChild($TestController.ImportNode($dnode, $true))
}
# Append the default attributes that do not exist in the computer specific sections
else
{
Write-Verbose "Appending default attributes that don't exist in computer nodes..."
foreach ($attribute in $dnode.Attributes)
{
if (-not $cnode.HasAttribute($attribute.Name))
{
Write-Verbose "Adding attribute '$($attribute.Name)=$($attribute.Value)' to the computer node."
$cnode.SetAttribute($attribute.Name, $attribute.Value)
}
}
}
}

Write-Verbose "***** After adding default attributes *****"
Write-Verbose "$($computerNode.OuterXml)"
Write-Verbose "************************************************************"

$computers += $computerNode
}

return $computers
}

最佳答案

我采用了您的示例函数和示例数据。运行该函数,我得到了您描述的输出。

PS C:\Users\Matt> (Read-TestControllerXml C:\temp\xml.txt).GetType().FullName
System.Xml.XmlElement

然后我将一个计算机节点添加到示例文件并再次运行该函数。

PS C:\Users\Matt> (Read-TestControllerXml C:\temp\xml.txt).GetType().FullName
System.Object[]

这是正确的,因为您的函数返回对象数组。如果你分解项目并检查它们的类型,它应该是有意义的。

PS C:\Users\Matt> Read-TestControllerXml C:\temp\xml.txt | ForEach-Object{$_.GetType().FullName}
System.Xml.XmlElement
System.Xml.XmlElement

我有两个项目并将它们放入 ForEach-Object 循环中并分别检查它们的类型。由于您的输出是一个数组,因此期望有多个对象。总之,您的函数总是返回 [System.Xml.XmlElement] 只是当存在多个时它是一个 System.Object[] 数组。

出于好奇

我试图强制 $computers 成为像这样的 xml 元素数组 $computers = [System.Xml.XmlElement[]]@()。它没有改变输出。多个项目仍作为 System.Object[]

输出

关于xml - 为什么这个 Powershell 函数有时返回一个列表而其他时候返回一个 XmlElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25943285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com