Powershell:嵌套XML元素是字符串类型而不是XmlElement

时间:2015-08-03 12:46:39

标签: powershell types casting

[xml]$xml = @"
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <elementLevel1>
        <elementLevel2>text node 1</elementLevel2>
        <elementLevel2>text node 2</elementLevel2>
        <elementLevel2>text node 3</elementLevel2>
        <elementLevel2>text node 4</elementLevel2>
    </elementLevel1>
    <elementLevel1>
        <elementLevel2>text node 1</elementLevel2>
        <elementLevel2>text node 2</elementLevel2>
        <elementLevel2>text node 3</elementLevel2>
        <elementLevel2>text node 4</elementLevel2>
    </elementLevel1>
</root>
"@

foreach($level1 in $xml.root.elementLevel1)
{
    Write-Host("Type of elementLevel1: " + $level1.GetType())
    Write-Host("Type of elementLevel2 in outer loop: " + $level1.elementLevel2.GetType())
    foreach($level2 in $level1.elementLevel2)
    {
        Write-Host("Type of elementLevel2 in inner loop: " + $level2.GetType())
    }
}

收率:

Type of elementLevel1: System.Xml.XmlElement
Type of elementLevel2 in outer loop: System.Object[]
Type of elementLevel2 in inner loop: string
Type of elementLevel2 in inner loop: string
Type of elementLevel2 in inner loop: string
Type of elementLevel2 in inner loop: string
Type of elementLevel1: System.Xml.XmlElement
Type of elementLevel2 in outer loop: System.Object[]
Type of elementLevel2 in inner loop: string
Type of elementLevel2 in inner loop: string
Type of elementLevel2 in inner loop: string
Type of elementLevel2 in inner loop: string

我的最终目标是更改每个elementLevel2的文本节点,但是您无法修改字符串的InnerXml属性,也无法将字符串强制转换为[System.Xml.XmlElement]。如何强制类型系统认为elementLevel2是一个XmlElement?

1 个答案:

答案 0 :(得分:3)

您可以使用SelectNodesQListWidgetItem* lwi = new QListWidgetItem(QIcon(":Image/pinwheel.png"), "Цвета"); lwi->setSizeHint(QSize(256, 52)); listWidget->addItem( lwi ); lwi->setTextAlignment(Qt::AlignCenter); lwi = new QListWidgetItem(QIcon(":Image/pinwheel.png"), "Управление и что-то еще"); lwi->setSizeHint(QSize(256, 52)); listWidget->addItem( lwi ); lwi->setTextAlignment(Qt::AlignCenter); 来选择您的节点并对其进行迭代:

xpath