如何使用powershell将数据插入XML childelement [duplicate]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用powershell将数据插入XML childelement [duplicate]相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

我有以下xml(Main.xml):

    <AssignedAccessConfiguration xmlns="http://schemas.microsoft.com/AssignedAccess/2017/config">
  <Profiles>
    <Profile Id="">
      <AllAppsList>
        <AllowedApps />
      </AllAppsList>
      <StartLayout />
      <Taskbar ShowTaskbar="false" />
      <Configs>
        <Config>
          <Account>
            <DefaultProfile Id="" />
          </Account>
        </Config>
      </Configs>
    </Profile>
  </Profiles>
</AssignedAccessConfiguration>

我需要在Powershell的开始和结束“StartLayout”标签之间添加内容。看起来像这样:

<StartLayout>
        <![CDATA[<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
                      <LayoutOptions StartTileGroupCellWidth="6" />
                      <DefaultLayoutOverride>
                        <StartLayoutCollection>
                          <defaultlayout:StartLayout GroupCellWidth="6">
                            <start:Group Name="Group1">
                              <start:Tile Size="4x4" Column="0" Row="0" AppUserModelID="Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic" />
                              <start:Tile Size="2x2" Column="4" Row="2" AppUserModelID="Microsoft.ZuneVideo_8wekyb3d8bbwe!Microsoft.ZuneVideo" />
                              <start:Tile Size="2x2" Column="4" Row="0" AppUserModelID="Microsoft.Windows.Photos_8wekyb3d8bbwe!App" />
                              <start:Tile Size="2x2" Column="4" Row="4" AppUserModelID="Microsoft.BingWeather_8wekyb3d8bbwe!App" />
                              <start:Tile Size="4x2" Column="0" Row="4" AppUserModelID="Microsoft.WindowsCalculator_8wekyb3d8bbwe!App" />
                            </start:Group>
                            <start:Group Name="Group2">
                              <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%MicrosoftWindowsStart MenuProgramsAccessoriesPaint.lnk" />
                              <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%APPDATA%MicrosoftWindowsStart MenuProgramsAccessoriesNotepad.lnk" />
                            </start:Group>
                          </defaultlayout:StartLayout>
                        </StartLayoutCollection>
                      </DefaultLayoutOverride>
                    </LayoutModificationTemplate>
                ]]>
</StartLayout>

我已经将“StartLayout”内容称为“Layout.xml”,它是作为单独的XML生成的:

<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
  <DefaultLayoutOverride LayoutCustomizationRestrictionType="OnlySpecifiedGroups">
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="6">
        <start:Group Name="CoWorkerApps">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationID="{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}cmd.exe" />
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationID="Microsoft.Windows.Shell.RunDialog" />
          <start:DesktopApplicationTile Size="2x2" Column="4" Row="0" DesktopApplicationID="{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}WindowsPowerShellv1.0powershell.exe" />
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="2" DesktopApplicationID="{6D809377-6AF0-444B-8957-A3773F02200E}Windows NTAccessorieswordpad.exe" />
        </start:Group>
        <start:Group Name="CustomerApps">
          <start:Tile Size="2x2" Column="0" Row="0" AppUserModelID="Microsoft.WindowsCalculator_8wekyb3d8bbwe!App" />
          <start:Tile Size="2x2" Column="2" Row="0" AppUserModelID="Microsoft.Messaging_8wekyb3d8bbwe!x27e26f40ye031y48a6yb130yd1f20388991ax" />
          <start:Tile Size="2x2" Column="4" Row="0" AppUserModelID="Microsoft.XboxApp_8wekyb3d8bbwe!Microsoft.XboxApp" />
          <start:Tile Size="2x2" Column="0" Row="2" AppUserModelID="Microsoft.Getstarted_8wekyb3d8bbwe!App" />
        </start:Group>
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
</LayoutModificationTemplate>

我添加了CDATA:

C:Windowssystem32> [xml]$xml =(gc "C:StartMenuLayoutFIX.xml")
PS C:Windowssystem32> $test = ($xml.CreateCDataSection($testMe).OuterXml)
PS C:Windowssystem32> $test
<![CDATA[<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schem
as.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification"><DefaultLayou
tOverride LayoutCustomizationRestrictionType="OnlySpecifiedGroups"><StartLayoutCollection><defaultlayout:StartLayout GroupCellWidth="
6"><start:Group Name="CustomerApps" /><start:Group Name="CoWorkerApps" /></defaultlayout:StartLayout></StartLayoutCollection></Defaul
tLayoutOverride></LayoutModificationTemplate>]]>

问题是:如何将内容“Layout.xml”添加到Main.xml中的“StartLayout”标记,最好是使用powershell?

只是为了测试,我一直在玩:

element = $xmlDocument.CreateElement("RootElement")

$subElement = $xmlDocument.CreateElement("SubElement")

#Note the XML can only contain Text (String) based values. Which means that my $null variable will not work!!!

$subElement.SetAttribute($null, $AddStartLayout)
#$subElement.SetAttribute("SomeValue", "True")    

$element.AppendChild($subElement)

#This appends my newly created Sub Element to the document RootElement

#Creates a base element called RootElement

$xmlDocument.AppendChild($element)

#Append the XML RootElement to the XML Document

$xmlDocument.Save("$($LayoutPath)$LayoutXMLFile")

方法SetAttribute()需要两个字符串值。虽然Main.xml中的子元素“StartLayout”只有内容(带有CDATA的Layout.xml)。

有没有人知道如何只用数据注入“StartLayout”子元素(任何语言都会这样做。我只需要尝试将其翻译成powershell)。

非常感谢任何提示。

答案

这应该做的伎俩:

[xml]$main = @"
   <AssignedAccessConfiguration xmlns="http://schemas.microsoft.com/AssignedAccess/2017/config">
  <Profiles>
    <Profile Id="">
      <AllAppsList>
        <AllowedApps />
      </AllAppsList>
      <StartLayout />
      <Taskbar ShowTaskbar="false" />
      <Configs>
        <Config>
          <Account>
            <DefaultProfile Id="" />
          </Account>
        </Config>
      </Configs>
    </Profile>
  </Profiles>
</AssignedAccessConfiguration>
"@

[xml]$startLayout = @"
<StartLayout>
        <![CDATA[<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
                      <LayoutOptions StartTileGroupCellWidth="6" />
                      <DefaultLayoutOverride>
                        <StartLayoutCollection>
                          <defaultlayout:StartLayout GroupCellWidth="6">
                            <start:Group Name="Group1">
                              <start:Tile Size="4x4" Column="0" Row="0" AppUserModelID="Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic" />
                              <start:Tile Size="2x2" Column="4" Row="2" AppUserModelID="Microsoft.ZuneVideo_8wekyb3d8bbwe!Microsoft.ZuneVideo" />
                              <start:Tile Size="2x2" Column="4" Row="0" AppUserModelID="Microsoft.Windows.Photos_8wekyb3d8bbwe!App" />
                              <start:Tile Size="2x2" Column="4" Row="4" AppUserModelID="Microsoft.BingWeather_8wekyb3d8bbwe!App" />
                              <start:Tile Size="4x2" Column="0" Row="4" AppUserModelID="Microsoft.WindowsCalculator_8wekyb3d8bbwe!App" />
                            </start:Group>
                            <start:Group Name="Group2">
                              <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%MicrosoftWindowsStart MenuProgramsAccessoriesPaint.lnk" />
                              <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%APPDATA%MicrosoftWindowsStart MenuProgramsAccessoriesNotepad.lnk" />
                            </start:Group>
                          </defaultlayout:StartLayout>
                        </StartLayoutCollection>
                      </DefaultLayoutOverride>
                    </LayoutModificationTemplate>
                ]]>
</StartLayout>
"@

$main.ChildNodes.Profiles.Profile.AppendChild( $main.ImportNode( $startLayout.StartLayout, $true ) )

以上是关于如何使用powershell将数据插入XML childelement [duplicate]的主要内容,如果未能解决你的问题,请参考以下文章

powershell 从powershell将数据插入表中

添加位于多级的 XML 内容 - PowerShell 函数

如何使用XML文件中的PowerShell更新数据源连接字符串

如何将 XML 中的数据插入数据库 [重复]

如何在 C# 控制台应用程序中使用 XmlTextReader 将 XML 数据插入 SQL Server 表?

如何将xml数据插入数据库