Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用powershell创建一个XML结构的多个变体?_Xml_Powershell - Fatal编程技术网

如何使用powershell创建一个XML结构的多个变体?

如何使用powershell创建一个XML结构的多个变体?,xml,powershell,Xml,Powershell,因此,我的主要目标是能够创建XML元素的多个变体(如果可能,使用powershell)。例如,下面的xml结构: <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 托弗 贾尼 提醒 这个周

因此,我的主要目标是能够创建XML元素的多个变体(如果可能,使用powershell)。例如,下面的xml结构:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>

</note>

托弗
贾尼
提醒
这个周末别忘了我!
问题1:有没有办法将整个结构保存在一个变量中

问题2:我如何“保存”该结构并仅通过一次修改(如将Jani更改为cody and John)创建该结构的多个副本?我希望能够随意制作该结构的修改副本,但不知道从哪里开始


谢谢你的帮助。谢谢大家!

使用带有占位符的Here字符串

$xmlTemplate = @"
<note>
    <to>{0}</to>
    <from>{1}</from>
    <heading>{2}</heading>
    <body>{3}</body>
</note>
"@
结果:

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
<note>
    <to>Bloggs</to>
    <from>Cody</from>
    <heading>Cancellation</heading>
    <body>No can do!</body>
</note>
<note>
    <to>Doe</to>
    <from>John</from>
    <heading>Information</heading>
    <body>How about next weekend?</body>
</note>

托弗
贾尼
提醒
这个周末别忘了我!
布洛格斯
科迪
取消
不行!
雌鹿
约翰
问询处
下周末怎么样?

整洁!快速(希望如此)提问:将所有这些片段保存到一个组合xml文件中的最简单方法是什么?@JackFleeting。Caprure结果如
$result=$messages.GetEnumerator()
所示,将其包含在根元素中并另存为.xmlYou是对的,但我想您是想将注释发送给OP,而不是我。。。我已经对答案投了赞成票。@JackFleeting啊,很抱歉。我懒得向上滚动查看OP的名字。。谢谢你的投票(顺便说一句;)谢谢这是一个令人惊叹的新事物,因此您可能不知道这一点,但习惯于单击✓ 左边的图标。这将帮助其他有类似问题的人更容易找到它,并有助于激励人们回答你的问题。
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
<note>
    <to>Bloggs</to>
    <from>Cody</from>
    <heading>Cancellation</heading>
    <body>No can do!</body>
</note>
<note>
    <to>Doe</to>
    <from>John</from>
    <heading>Information</heading>
    <body>How about next weekend?</body>
</note>