如何使用PowerShell重命名XML节点?

时间:2022-06-24 07:15:06

I'm trying to rename an XML node using PowerShell. For example:
<configuration>
<desktops>
<name>PC001</name>
<domain>CORP</domain>
</desktops>
<laptops>
<name>PC002</name>
<domain>CORP</domain>
</laptops>
</configuration>

我正在尝试使用PowerShell重命名XML节点。例如: PC001 CORP PC002 CORP

I want to rename the first <name> tags to <PC1name> (and </PC1name> respectively). Here's what I have, so far:

我想将第一个 标记重命名为 (和 )。到目前为止,这就是我所拥有的:

$InputFile = "NetworkConfigs.xml"
$xml = [xml](get-content $InputFile)
$root = $xml.get_DocumentElement();
#replace the node
$root.desktops.name.?`

$ InputFile =“NetworkConfigs.xml”$ xml = [xml](get-content $ InputFile)$ root = $ xml.get_DocumentElement(); #replace the node $ root.desktops.name。?`

$xml.Save($InputFile)

$ xml.Save($ INPUTFILE)

I don't know how to replace the tag with something else. Tips?

我不知道如何用其他东西替换标签。提示?

3 个解决方案

#1


7  

Bottom line, an XML node's name is immutable. Reference msdn.

最重要的是,XML节点的名称是不可变的。参考msdn。

Here's a quick example of creating a new node with the required data. Hope it helps.

以下是使用所需数据创建新节点的快速示例。希望能帮助到你。

$InputText = @"
<configuration>
<desktops>
<name>PC001</name>
<domain>CORP</domain>
</desktops>
<laptops>
<name>PC002</name>
<domain>CORP</domain>
</laptops>
</configuration>
"@

$xml = [xml]($inputText)
$desktopsNode = [System.Xml.XmlElement]$xml.configuration.desktops
$nameNode = $desktopsNode.SelectSingleNode('name')
$pcNameNode = $xml.CreateElement('PC1Name')
$pcNameNode.InnerText = $nameNode.InnerText
[void]$desktopsNode.AppendChild($pcNameNode)
[void]$desktopsNode.RemoveChild($nameNode)
$xml.OuterXML

Output:

输出:

<configuration><desktops><domain>CORP</domain><PC1Name>PC001</PC1Name></desktops><laptops><name>PC002</n
ame><domain>CORP</domain></laptops></configuration>

#2


3  

Renaming nodes in XML is more complicated than you might expect. It's especially bad if the node is a root node, or parent with a complex hierarchy of child nodes. Most "rename" methods I've seen will clone the children and append them to the new node. This process is made a little easier if your API also includes a ReplaceChild method. (I can provide details if you need them.)

在XML中重命名节点比您预期的更复杂。如果节点是根节点,或者父节点具有复杂的子节点层次结构,则尤其如此。我见过的大多数“重命名”方法都会克隆子节点并将它们附加到新节点。如果您的API还包含ReplaceChild方法,则此过程会更容易一些。 (如果您需要,我可以提供详细信息。)

An alternative method that I have used (especially if the XML can be represented as a string) is to replace the text in the XML before converting it to XmlDocument.

我使用的另一种方法(特别是如果XML可以表示为字符串)是在将XML转换为XmlDocument之前替换XML中的文本。

$InputText = @"
<configuration>
<desktops>
<name>PC001</name>
<domain>CORP</domain>
</desktops>
<laptops>
<name>PC002</name>
<domain>CORP</domain>
</laptops>
</configuration>
"@

$regex = [regex]'(</?)name>'
$ModifiedText = $regex.Replace($InputText,"`$1PC1Name>",2)
$xml = [xml]$ModifiedText

Note that the replace statement finds and fixes the first 2 occurrences of the match--the opening and closing tag of the first element only. Remove the number to find and replace all occurrences in the string. Note also that the regular expression captures the opening tag characters, so that they can be inserted into the string match as $1.

请注意,replace语句查找并修复匹配的前两个匹配项 - 仅第一个元素的开始和结束标记。删除数字以查找并替换字符串中的所有匹配项。另请注意,正则表达式捕获开始标记字符,以便它们可以作为$ 1插入到字符串匹配中。

#3


1  

$oldtag = "name"
$newtag = "PC1name"
$xml = Get-Content D:\oldfile.xml
$new = $xml -replace $oldtag, $newtag
Set-content -path D:\newfile.xml -value $new

My way is I convert the XML to a string, then replace the node (which in this case is just normal string). It works for me.

我的方法是将XML转换为字符串,然后替换节点(在这种情况下只是普通字符串)。这个对我有用。

#1


7  

Bottom line, an XML node's name is immutable. Reference msdn.

最重要的是,XML节点的名称是不可变的。参考msdn。

Here's a quick example of creating a new node with the required data. Hope it helps.

以下是使用所需数据创建新节点的快速示例。希望能帮助到你。

$InputText = @"
<configuration>
<desktops>
<name>PC001</name>
<domain>CORP</domain>
</desktops>
<laptops>
<name>PC002</name>
<domain>CORP</domain>
</laptops>
</configuration>
"@

$xml = [xml]($inputText)
$desktopsNode = [System.Xml.XmlElement]$xml.configuration.desktops
$nameNode = $desktopsNode.SelectSingleNode('name')
$pcNameNode = $xml.CreateElement('PC1Name')
$pcNameNode.InnerText = $nameNode.InnerText
[void]$desktopsNode.AppendChild($pcNameNode)
[void]$desktopsNode.RemoveChild($nameNode)
$xml.OuterXML

Output:

输出:

<configuration><desktops><domain>CORP</domain><PC1Name>PC001</PC1Name></desktops><laptops><name>PC002</n
ame><domain>CORP</domain></laptops></configuration>

#2


3  

Renaming nodes in XML is more complicated than you might expect. It's especially bad if the node is a root node, or parent with a complex hierarchy of child nodes. Most "rename" methods I've seen will clone the children and append them to the new node. This process is made a little easier if your API also includes a ReplaceChild method. (I can provide details if you need them.)

在XML中重命名节点比您预期的更复杂。如果节点是根节点,或者父节点具有复杂的子节点层次结构,则尤其如此。我见过的大多数“重命名”方法都会克隆子节点并将它们附加到新节点。如果您的API还包含ReplaceChild方法,则此过程会更容易一些。 (如果您需要,我可以提供详细信息。)

An alternative method that I have used (especially if the XML can be represented as a string) is to replace the text in the XML before converting it to XmlDocument.

我使用的另一种方法(特别是如果XML可以表示为字符串)是在将XML转换为XmlDocument之前替换XML中的文本。

$InputText = @"
<configuration>
<desktops>
<name>PC001</name>
<domain>CORP</domain>
</desktops>
<laptops>
<name>PC002</name>
<domain>CORP</domain>
</laptops>
</configuration>
"@

$regex = [regex]'(</?)name>'
$ModifiedText = $regex.Replace($InputText,"`$1PC1Name>",2)
$xml = [xml]$ModifiedText

Note that the replace statement finds and fixes the first 2 occurrences of the match--the opening and closing tag of the first element only. Remove the number to find and replace all occurrences in the string. Note also that the regular expression captures the opening tag characters, so that they can be inserted into the string match as $1.

请注意,replace语句查找并修复匹配的前两个匹配项 - 仅第一个元素的开始和结束标记。删除数字以查找并替换字符串中的所有匹配项。另请注意,正则表达式捕获开始标记字符,以便它们可以作为$ 1插入到字符串匹配中。

#3


1  

$oldtag = "name"
$newtag = "PC1name"
$xml = Get-Content D:\oldfile.xml
$new = $xml -replace $oldtag, $newtag
Set-content -path D:\newfile.xml -value $new

My way is I convert the XML to a string, then replace the node (which in this case is just normal string). It works for me.

我的方法是将XML转换为字符串,然后替换节点(在这种情况下只是普通字符串)。这个对我有用。