2013-05-29 34 views
7

是否可以注释掉XDocument中的节点?如何使用PowerShell注释XML中的节点?

我有以下标签。

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

那么我该如何处理这种情况?

+0

每种语言都有自己的评论方式。在Powershell中它是'#',C中是'''',[XML有其自己的方式](http://www.w3schools.com/xml/xml_syntax.asp) - 见下面的答案。不要认为它是“//”或“#”(无论你最习惯什么)。 – Neolisk

回答

4

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> 
+0

非常感谢。它评论节点。但我看到的一件事是,由于节点属于一个名称空间,因此Node.outXml包含名称空间,该名称空间附加在节点的末尾。但这不是预期的结果。我可以阻止该名称空间出现在Node.OuterXml中。 – user1595214

+0

你是样本没有包含任何命名空间。如果您需要帮助,请提供有效的xml示例,并且您尝试过的代码不起作用。 –

+0

我的XML如下:' '<的ServiceFactory> <连接名称=” TIM”跟踪= “全部”> <属性名= “AVEVA.Components.TransactionSupportEnabled” 值= “真”/> '我使用了与上面所写的完全相同的代码,但coe中的node.Outerxml调用也附加了xml的名称空间。 – user1595214

11

您可以简单地创建一个注释节点,以及与这些评论取代你的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> 
3

这里是一个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"); 
} 

这里是一个PowerShell功能取消注释XML节点

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" 
+0

谢谢你..我想知道如果你找到了一种评论方式,同时保留格式。 –

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) 
相关问题