2013-05-29 22 views
7

XDocument'deki bir düğümü yorumlamak mümkün mü?PowerShell kullanarak XML'deki bir düğümü nasıl yorumlayabilirim?

Aşağıdaki etikete sahibim.

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

Düğümü kaldırmak zorunda değilim; Sadece XML dosyasında yorumlanmış formatta olmasını istiyorum. Böyle bir şey kullanabilirsiniz:

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

Ama bir düğüm daha sonra

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

gibi iki hatlarında yayılırsa nasıl bu davayı ele?

+0

Her dilde bir şeyler yorumlama yolu vardır. Powershell'de '# ', C'de' // 've [XML'in kendi yolu vardır] (http://www.w3schools.com/xml/xml_syntax.asp) - aşağıya bakınız. Her yerde (en çok hangisi iseniz) '// 'veya' # 'olduğunu düşünmeyin. – Neolisk

cevap

4

Yorumlar, XML'de <!-- ve --> ile yapılır.

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

Çok teşekkür ederim.Notları yorumlamak.Ancak gördüğüm bir şey, düğümün bir ad alanına ait olması nedeniyle, Node.outXml düğümün sonuna eklenen Ad alanları içeriyor.Ancak bu, dışlanmış bir sonuç değil. Bu isim alanlarının Node.OuterXml'de görünmesini engelleyebilirim. – user1595214

+0

örneğiniz hiçbir isim alanını içermiyor. Yardıma ihtiyacınız varsa, geçerli bir xml örneği ve işe yaramayan kodunuzu sağlayın. –

+0

Benim XML'im şu şekildedir: ' ' <İşletme adı = "AVEVA.Components.TransactionSupportEnabled" value = "true" /> ' ve yukarıda yazdığınız gibi aynı kodu kullanıyorum ama codedaki node.Outerxml çağrısı xml ad alanını da ekliyor. – user1595214

11

Sadece bir açıklama düğümü oluşturabilir

$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 ve bu yorumu içeren abc düğümleri değiştirin: Bunu deneyin:

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

Çıkışlar:

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

Burada bir powershell işlevi Yorum bir xml düğümü için:

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öyle kullanabilirsiniz:

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

Uncomment xml düğüm bir powershell fonksiyonudur

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

-

UncommentXmlNode "D:\temp\file.xml" "Some String in the xml node to Uncomment" 
Biçimlendirmesini korumak istiyor ve yorumların etiketi içermez yorumladı Düğüm Değerini içeren bir geçici düğümünü inşa dize değiştirme

kullanmıyorsanız

+0

Bunun için teşekkürler .. Eğer merak ediyordum biçimlendirmeyi korurken yorum yapmanın bir yolunu buldu. –

0

Eğer alıntı bu bloğu kullanabilirsiniz, o zaman değişimi gerçekleştirmekten.

# 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) 
İlgili konular