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

时间:2013-05-29 17:19:42

标签: xml powershell

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

我有以下标签。

<abc key="test" value="samplevalue"></abc>

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

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

但如果一个节点分为两行,如

<abc key="test" value="sampleValue">
</abc>

然后我该如何处理这个案子?

4 个答案:

答案 0 :(得分:13)

您可以简单地创建一个注释节点,并用这些注释替换您的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 #>);

输出:

<root><!--<abc key="test" value="samplevalue"></abc>--><!--<abc key="sa" value="dsad">sda
</abc>--></root>

答案 1 :(得分:5)

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")

的test.xml

<root>
  <!--<abc key="test" value="samplevalue"></abc>-->
  <abc key="sa" value="dsad">sda
</abc>
</root>

答案 2 :(得分:4)

这是一个powershell函数,用于注释xml节点

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");
}

这是取消注释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"
        }
    }

}

您可以像这样使用它们:

CommentXmlNode "D:\temp\file.xml" "YourXPath"

-

UncommentXmlNode "D:\temp\file.xml" "Some String in the xml node to Uncomment"

答案 3 :(得分:0)

如果您想保留格式并且不使用字符串替换

,则可以使用此引用块

它构建一个包含注释节点值的临时节点,该节点不包含注释标记,然后进行替换。

# path to the file
$File = "c:\pathtoyourfile\example.xml"

# XPath to the element to un comment
$XPath = "/Settings/SettingNode[@name='Installation']/SettingNode[@name='Features']/Setting[@name='Features' and @scope='Installation']/StringArray/String"

# get xml file
$xmlFile = [xml](Get-Content $File) 

# get parent and child path from XPath
$parentXPath = $XPath.Substring(0, $XPath.LastIndexOf('/'))
$childXPath = $XPath -replace "$parentXPath/", ''

# get comment
$xmlNode = $xmlFile.SelectNodes("$parentXPath/comment()") | ? { $_.InnerText -match "<$childXPath" }

# create node containing comment content
$tempNode = $xmlFile.CreateElement("tempNode")          
$tempNode.InnerXml = $xmlNode.Value
$replaceNode = $tempNode.SelectSingleNode("/$childXPath")

# process replacement 
$xmlNode.ParentNode.ReplaceChild($replaceNode, $xmlNode)

# save change
$xmlFile.Save($File)