xml - 如何使用 powershell 创建一种 XML 结构的多种变体?
问题描述
所以我的主要目标是能够创建 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 和 John?我希望能够随意制作该结构的修改副本,但不知道从哪里开始。
任何帮助表示赞赏。谢谢!
解决方案
使用带有占位符的 Here-String 。
$xmlTemplate = @"
<note>
<to>{0}</to>
<from>{1}</from>
<heading>{2}</heading>
<body>{3}</body>
</note>
"@
然后使用它来创建任意数量的这些 xml 片段。占位符{0}
等使用Format 运算符{1}
获取其真实值,例如:-f
# this demo uses an array of Hashtables
$messages = @{To = 'Tove' ; From = 'Jani'; Heading = 'Reminder' ; Body = "Don't forget me this weekend!"},
@{To = 'Bloggs'; From = 'Cody'; Heading = 'Cancellation'; Body = "No can do!"},
@{To = 'Doe' ; From = 'John'; Heading = 'Information' ; Body = "How about next weekend?"}
$messages.GetEnumerator() | ForEach-Object {
$xmlTemplate -f $_.To, $_.From, $_.Heading, $_.Body
}
结果:
<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>
推荐阅读
- ios - 快速更新键盘预测栏建议
- python - 如何为我的 __dict__ 属性推广此功能
- angular - 新 Ionic/Angular 应用程序的 tfignore 问题
- android - onResume() 更改菜单项出错
- c - execve /bin/bash 作为子进程。使用管道通过父进程与 bash 交互
- excel - 根据excel vba中的不同标准计算平均值
- google-cloud-platform - 对话框流中的错误 getaddrinfo ENOTFOUND
- r - 乳胶 R 块输出未修饰
- python - 如何通过随机搜索优化 SVM 的权重?
- scala - Scala 调试这个递归示例