Is it possible to comment out a node in an XDocument?
是否可以注释掉XDocument中的节点?
I have following tag.
我有以下标签。
<abc key="test" value="samplevalue"></abc>
I don't have to remove the node; I just want it to be there in the XML file in commented format. I can use something like this:
我没有删除节点;我只是希望它以注释格式存在于XML文件中。我可以使用这样的东西:
$node = $xml.selectSingleNode('//abc')
#$node.OuterXml.Insert(0,"#");
$node.$xml.Save("c:\test.xml")
But if one node spreads in two lines like
但如果一个节点像两条线一样传播
<abc key="test" value="sampleValue">
</abc>
then how do I handle this case?
那我该如何处理这个案子呢?
4 个解决方案
#1
4
Comments in XML are done with <!--
and -->
. Try this:
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
的test.xml
<root>
<!--<abc key="test" value="samplevalue"></abc>-->
<abc key="sa" value="dsad">sda
</abc>
</root>
#2
12
You can simply create a comment node, and replace your abc nodes with these comments:
您只需创建一个注释节点,并用这些注释替换您的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 #>);
Outputs:
输出:
<root><!--<abc key="test" value="samplevalue"></abc>--><!--<abc key="sa" value="dsad">sda
</abc>--></root>
#3
3
Here a powershell function to Comment an xml node:
这里有一个用于注释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");
}
Here is a powershell function to Uncomment xml node:
这是一个取消注释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"
}
}
}
You can use them like this :
您可以像这样使用它们:
CommentXmlNode "D:\temp\file.xml" "YourXPath"
--
-
UncommentXmlNode "D:\temp\file.xml" "Some String in the xml node to Uncomment"
#4
0
you can use this block of quote if you want to preserve formatting and don't use string replacement
如果要保留格式并且不使用字符串替换,则可以使用此引用块
it build a temp node containing the commented Node Value, which does not include comments tag, then it process replacement.
它构建一个包含注释节点值的临时节点,该节点不包含注释标记,然后进行替换。
# 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)
#1
4
Comments in XML are done with <!--
and -->
. Try this:
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
的test.xml
<root>
<!--<abc key="test" value="samplevalue"></abc>-->
<abc key="sa" value="dsad">sda
</abc>
</root>
#2
12
You can simply create a comment node, and replace your abc nodes with these comments:
您只需创建一个注释节点,并用这些注释替换您的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 #>);
Outputs:
输出:
<root><!--<abc key="test" value="samplevalue"></abc>--><!--<abc key="sa" value="dsad">sda
</abc>--></root>
#3
3
Here a powershell function to Comment an xml node:
这里有一个用于注释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");
}
Here is a powershell function to Uncomment xml node:
这是一个取消注释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"
}
}
}
You can use them like this :
您可以像这样使用它们:
CommentXmlNode "D:\temp\file.xml" "YourXPath"
--
-
UncommentXmlNode "D:\temp\file.xml" "Some String in the xml node to Uncomment"
#4
0
you can use this block of quote if you want to preserve formatting and don't use string replacement
如果要保留格式并且不使用字符串替换,则可以使用此引用块
it build a temp node containing the commented Node Value, which does not include comments tag, then it process replacement.
它构建一个包含注释节点值的临时节点,该节点不包含注释标记,然后进行替换。
# 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)