Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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

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
Xml 选择与Powershell有注释的模块_Xml_Powershell_Pom.xml - Fatal编程技术网

Xml 选择与Powershell有注释的模块

Xml 选择与Powershell有注释的模块,xml,powershell,pom.xml,Xml,Powershell,Pom.xml,我想选择有注释行的模块 <modules> <module>com.umut.web</module><!--auto--> <module>com.umut.oaservice</module> <module>com.umut.signaturephotoservice</module> </modules> 我认为你的foreach的水平太高了 如果您这

我想选择有注释行的模块

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

我认为你的
foreach
的水平太高了

如果您这样做:

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

我认为您要问的是如何在xml中找到真正有注释元素的
,对吗

在这种情况下,下面是一些可能的方法:

[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
        }
    }
}

是的,我现在看到自动输出。但我还需要另一个值,它是同一行。对于本例,我还需要com.umut.web此值。我看到的问题是
#comment
属性未链接到实际注释所在的行。[皱眉]我试着把它移到第二行&它仍然显示为唯一的评论,而不是第二条评论。这使得您似乎无法使用XML的东西将注释与它应该寻址的模块连接起来。是的,我认为我无法在这方面帮助您。我建议重新编制该文件。搜索属性和值要简单得多。
[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
        }
    }
}
module moduleText         comment  commentText
------ ----------         -------  -----------
module com.umut.oaservice #comment auto