2013-05-29 31 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) 
相關問題