13

是否可以注释掉 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

4 回答 4

17

您可以简单地创建一个评论节点,并用这些评论替换您的 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>
于 2013-05-29T18:57:22.393 回答
8

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

测试.xml

<root>
  <!--<abc key="test" value="samplevalue"></abc>-->
  <abc key="sa" value="dsad">sda
</abc>
</root>
于 2013-05-29T18:08:42.173 回答
6

这是一个用于注释 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");
}

这是取消注释 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"
于 2015-03-20T20:57:51.143 回答
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)
于 2015-10-14T08:41:55.273 回答