[英]Read and Edit XML content and get output to an XML variable in PowerShell
我有一个类似于以下内容的XML文件内容:
<?xml version="1.0" encoding="utf-8"?>
<Content>
<FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID>
<File Path="C:\test.config">
<Tag TagName="configuration">
<add Content="One" fileID="${FileID}"/>
<secondnode Content="Two" fileID="${FileID}">
<nodeinside>
<inneragain fileID="${FileID}"/>
</nodeinside>
</secondnode>
...
<diffentnode Content="Infinite" fileID="${FileID}"/>
</Tag>
</File>
</Content>
我只需要获取此XML文件的内容并进行编辑(替换$ {FileID}所在的位置),如下所示
<add fileID="${FileID}"/>
使用以下行中“ FileID”的值并将输出输出到DataType [xml]的变量。
<FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID>
请提出可以在PowerShell中完成此操作的最佳方法。
PS H:\> $xml = [xml]'<?xml version="1.0" encoding="utf-8"?>
<Content>
<FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID>
<File Path="C:\test.config">
<Tag TagName="configuration">
<add Content="One" fileID="${FileID}"/>
<secondnode Content="Two" fileID="${FileID}">
<nodeinside>
<inneragain fileID="${FileID}"/>
</nodeinside>
</secondnode>
<diffentnode Content="Infinite" fileID="${FileID}"/>
</Tag>
</File>
</Content>'
$xml.InnerXml.Replace('${FileID}',$xml.Content.FileID)
<?xml version="1.0" encoding="utf-8"?><Content><FileID>109F2AEA-6D9C-4127-963A-9C71D4155C5D</FileID><File Path="C:\test.config"><Tag TagName="configuration"><add Content="One" fileID="109F2AEA-6D9C-4127-963A-9C71D4155C5D" /><secon
dnode Content="Two" fileID="109F2AEA-6D9C-4127-963A-9C71D4155C5D"><nodeinside><inneragain fileID="109F2AEA-6D9C-4127-963A-9C71D4155C5D" /></nodeinside></secondnode><diffentnode Content="Infinite" fileID="109F2AEA-6D9C-4127-963A-9C
71D4155C5D" /></Tag></File></Content>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.