如何使用PowerShell注释掉XML中的节点?

use*_*214 10 xml powershell

是否可以注释掉XDocument中的节点?

我有以下标签.

<abc key="test" value="samplevalue"></abc>
Run Code Online (Sandbox Code Playgroud)

我没有删除节点; 我只是希望它以注释格式存在于XML文件中.我可以使用这样的东西:

$node = $xml.selectSingleNode('//abc')
#$node.OuterXml.Insert(0,"#");
$node.$xml.Save("c:\test.xml")
Run Code Online (Sandbox Code Playgroud)

但如果一个节点像两条线一样传播

<abc key="test" value="sampleValue">
</abc>
Run Code Online (Sandbox Code Playgroud)

那我该如何处理这个案子呢?

Mar*_*cus 14

您只需创建一个注释节点,并用这些注释替换您的abc节点:

$xml = [xml]@"
<root>
<abc key="test" value="samplevalue"></abc>
<abc key="sa" value="dsad">sda
</abc>
</root>
"@;

$xml.SelectNodes("//abc") | ForEach-Object { 
    $abc = $_;
    $comment = $xml.CreateComment($abc.OuterXml);
    $abc.ParentNode.ReplaceChild($comment, $abc);
}

$xml.Save(<# filename #>);
Run Code Online (Sandbox Code Playgroud)

输出:

<root><!--<abc key="test" value="samplevalue"></abc>--><!--<abc key="sa" value="dsad">sda
</abc>--></root>
Run Code Online (Sandbox Code Playgroud)


Fro*_* F. 6

XML中的注释是用<!--和完成的-->.试试这个:

$xml = [xml]@"
<root>
<abc key="test" value="samplevalue"></abc>
<abc key="sa" value="dsad">sda
</abc>
</root>
"@

$node = $xml.selectSingleNode('//abc')
#OuterXML is read-only, so I took an alternative route
$node.ParentNode.InnerXml = $node.ParentNode.InnerXml.Replace($node.OuterXml, $node.OuterXml.Insert(0, "<!--").Insert($node.OuterXml.Length+4, "-->"))
$xml.Save("c:\test.xml")
Run Code Online (Sandbox Code Playgroud)

的test.xml

<root>
  <!--<abc key="test" value="samplevalue"></abc>-->
  <abc key="sa" value="dsad">sda
</abc>
</root>
Run Code Online (Sandbox Code Playgroud)


Ana*_*nas 5

这里是注释XML节点的powershell函数:

function CommentXmlNode([String] $filePath, [String] $nodeXPath)
{
    [xml]$xml = Get-Content -Path "$filePath"

    # Find the nodes that we want to comment
    $xml.SelectNodes("$nodeXPath") | ForEach-Object {

        $nodeToComment = $_;
        $comment = $xml.CreateComment($nodeToComment.OuterXml);

        # Comment the node
        $nodeToComment.ParentNode.ReplaceChild($comment, $nodeToComment);
    }

    # Save the file
    $xml.Save("$filePath");
}
Run Code Online (Sandbox Code Playgroud)

这是取消注释xml节点的powershell函数:

function UncommentXmlNode([String] $filePath, [String] $searchCriteria)
{    
    [xml]$xml = Get-Content -Path "$filePath"

    # Find all comments on the xml file
    $xml.SelectNodes("//comment()") | ForEach-Object {     

        # We convert the comment to an xml
        $nodeToConvert = $_;  
        $convertedNode = $nodeToConvert.InnerText | convertto-xml 
        [xml]$xmlConvertedNode = $convertedNode

        # Find the comment that match our search criteria
        $xmlConvertedNode.SelectNodes("/descendant::*[contains(text(), '$searchCriteria')]") | ForEach-Object { 

            $nodeToUncomment = $_;

            $strToFind = "<!--" + $nodeToUncomment.InnerText + "-->"

            $strReplacement = $nodeToUncomment.InnerText

            # Replace the commented string with uncommented one
            $con = Get-Content "$filePath"
            $con | % { $_.Replace($strToFind, $strReplacement) } | Set-Content "$filePath"
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

您可以像这样使用它们:

CommentXmlNode "D:\temp\file.xml" "YourXPath"
Run Code Online (Sandbox Code Playgroud)

-

UncommentXmlNode "D:\temp\file.xml" "Some String in the xml node to Uncomment"
Run Code Online (Sandbox Code Playgroud)