简体   繁体   中英

Select module which is the have the comment with Powershell

I want to select module which is the have the comment line .

<modules>
    <module>com.umut.web</module><!--auto-->
    <module>com.umut.oaservice</module>
    <module>com.umut.signaturephotoservice</module>
  </modules>

powershell code=

[string]$modules=""
foreach ($module in $xmlFile.project.modules.module) {

  Write-Output $module.'#comment'  

}

I think what you are asking is how to find the <module> that actually has the comment element directly behind it in the xml, right?

In that case, here is something that might do it:

[xml]$xmlFile = @"
<project>
    <modules>
        <module>com.umut.web</module>
        <module>com.umut.oaservice</module><!--auto-->
        <module>com.umut.signaturephotoservice</module>
    </modules>
</project>
"@

foreach ($node in $xmlFile.project.modules.ChildNodes) {
    if ($node.NodeType -eq 'Comment') {
        # if this comment node following a childnode within 'modules' 
        if ($node.PreviousSibling) {
            [PSCustomObject]@{
                'module'      = $node.PreviousSibling            # the module element (System.Xml.XmlElement object)
                'moduleText'  = $node.PreviousSibling.InnerText  # the text inside this module element
                'comment'     = $node                            # the comment element (System.Xml.XmlComment)
                'commentText' = $node.InnerText                  # the text inside the comment
            }
            break
        }
    }
}

It emits a PSCustomObject with four properties or nothing if the search did not yield any result.

 module moduleText comment commentText ------ ---------- ------- ----------- module com.umut.oaservice #comment auto 

I think you went a level too far in your foreach .

What if you do:

[string]$modules=""
foreach ($module in $xmlFile.project.modules) {
Write-Output $module.'#comment'  
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM