2013-05-29 32 views
7

Có thể nhận xét một nút trong một XDocument không?Làm cách nào để tôi có thể nhận xét một nút trong XML bằng PowerShell?

Tôi có thẻ sau.

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

Tôi không phải xóa nút; Tôi chỉ muốn nó ở đó trong tệp XML ở định dạng nhận xét. Tôi có thể sử dụng một cái gì đó như thế này:

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

Nhưng nếu một nút lây lan trong hai dòng như

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

sau đó làm thế nào để xử lý trường hợp này?

+0

Mọi ngôn ngữ đều có cách nhận xét riêng. Trong Powershell nó là '#', trong C nó là '//', và [XML có cách riêng của nó] (http://www.w3schools.com/xml/xml_syntax.asp) - xem câu trả lời dưới đây. Đừng cho rằng đó là '//' hoặc '#' ở mọi nơi (tùy thuộc vào bạn được sử dụng nhiều nhất). – Neolisk

Trả lời

4

Nhận xét bằng XML được thực hiện với <!---->. Hãy thử điều này:

$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

Cảm ơn bạn rất nhiều.Its bình luận các nodes.Nhưng một điều tôi thấy là vì nút thuộc về một không gian tên, Node.outXml chứa Namespaces nối vào cuối node.But đó không phải là kết quả exepected.Is có một cách tôi có thể ngăn chặn không gian tên đó xuất hiện trong Node.OuterXml. – user1595214

+0

bạn mẫu không bao gồm bất kỳ không gian tên nào. nếu bạn cần trợ giúp, hãy cung cấp mẫu xml hợp lệ và mã bạn đã thử không hoạt động. –

+0

XML của tôi như sau: ' ' ' và tôi đang sử dụng cùng một mã số như bạn đã viết ở trên, nhưng node.Outerxml gọi trong coe cũng sẽ thêm không gian tên của xml. – user1595214

11

Bạn chỉ có thể tạo ra một nút bình luận, và thay thế các nút abc của bạn với những ý kiến:

$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 #>); 

Đầu ra:

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

Đây là chức năng của PowerShell để Comment một nút 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"); 
} 

Đây là một chức năng PowerShell để nút xml Bỏ ghi chú:

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

} 

Bạn có thể sử dụng chúng như thế này:

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

-

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

Cảm ơn vì điều này .. Tôi đã tự hỏi nếu bạn tìm thấy một cách để bình luận trong khi vẫn giữ nguyên định dạng. –

0

bạn có thể sử dụng khối trích dẫn này nếu bạn muốn giữ nguyên định dạng và không sử dụng chuỗi thay thế

nó tạo nút tạm thời có chứa giá trị nhận xét, không bao gồm thẻ nhận xét.

# 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) 
Các vấn đề liên quan