简体   繁体   English

选择具有Powershell注释的模块

[英]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= powershell代码=

[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? 我想你要问的是如何找到实际上在xml后面直接有注释元素的<module> ,对吧?

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. 如果搜索没有产生任何结果,它会发出一个带有四个属性的PSCustomObject,或者没有任何内容。

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

I think you went a level too far in your foreach . 我认为你在你的foreach走得太远了。

What if you do: 如果你做了怎么办:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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