简体   繁体   English

修改 Appxmanifest - 通过 Powershell 将元素添加到 XML

[英]Modify Appxmanifest - Add Element to XML via Powershell

Right now I'm working on a Azure DevOps Building Pipeline and got stuck at manipulating the generated Package.appxmanifest in a way I need it.现在我正在研究 Azure DevOps Building Pipeline,并且一直坚持以我需要的方式操作生成的 Package.appxmanifest。

What I have:我有的:

<Package>
  <Capabilities>
    <uap2:Capability Name="spatialPerception" />
  </Capabilities>
</Package>

What I need:我需要的:

<Package>
  <Capabilities>
    <uap2:Capability Name="spatialPerception" />
    <rescap:Capability Name="perceptionSensorsExperimental" />
  </Capabilities>
</Package>

I tried to create an Element , add an Attribute to it and then add the Element as child to the Capabilities .我尝试创建一个Element ,向其添加一个Attribute ,然后将Element作为子元素添加到Capabilities But this generates wrong stuff.但这会产生错误的东西。 For example:例如:

PowerShell #1:电源外壳#1:

$newElement = $manifest.Package.Capabilities.AppendChild($manifest.CreateElement("rescap:Capability"))
$newElement.SetAttribute(“Name”,”perceptionSensorsExperimental”)

Generates #1:生成#1:

<Package>
  <Capabilities>
    <uap2:Capability Name="spatialPerception" />
    <Capability Name="perceptionSensorsExperimental" xmlns="" />
  </Capabilities>
</Package>

How can I create the mentioned line, consisting of rescap:Capability and the attribute Name="perceptionSensorsExperimental" ?如何创建上述行,由rescap:Capability和属性Name="perceptionSensorsExperimental"组成?

--Edit - 编辑
Initial Package.appxmanifest document in its entirety:完整的初始Package.appxmanifest文件:

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
         xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
         xmlns:uap2="http://schemas.microsoft.com/appx/manifest/uap/windows10/2"
         xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
         xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4"
         xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
         xmlns:mobile="http://schemas.microsoft.com/appx/manifest/mobile/windows10"
         ignorableNamespaces="uap uap2 uap3 uap4 mp mobile iot rescap"
         xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
         xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities">
  <Identity Name="XXXX" Publisher="CN=XXXX" Version="1.0.0.0" />
  <Properties>
    <DisplayName>XXXX</DisplayName>
    <PublisherDisplayName>XXXXX</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>
  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.10240.0" MaxVersionTested="10.0.22621.0" />
  </Dependencies>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="XXXX.App">
      <uap:VisualElements DisplayName="XXXXX" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="XXXX" BackgroundColor="transparent">
        <uap:DefaultTile ShortName="XXXX" Wide310x150Logo="Assets\Wide310x150Logo.png" />
        <uap:SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="#FFFFFF" />
        <uap:InitialRotationPreference>
          <uap:Rotation Preference="landscape" />
          <uap:Rotation Preference="landscapeFlipped" />
          <uap:Rotation Preference="portrait" />
          <uap:Rotation Preference="portraitFlipped" />
        </uap:InitialRotationPreference>
      </uap:VisualElements>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="internetClientServer" />
    <Capability Name="privateNetworkClientServer" />
    <uap:Capability Name="sharedUserCertificates" />
    <uap2:Capability Name="spatialPerception" />
    <DeviceCapability Name="webcam" />
    <DeviceCapability Name="microphone" />
    <DeviceCapability Name="gazeinput" />
  </Capabilities>
</Package>

Using Xml Linq:使用 Xml Linq:

using assembly System 
using assembly System.Linq
using assembly System.Xml.Linq 

$inputFilename = "c:\temp\test.xml"
$outputFilename = "c:\temp\test1.xml"
$xDoc = [System.Xml.Linq.XDocument]::Load($inputFilename)
$root = [System.Xml.Linq.XElement]$xDoc.Root
#Write-Host "root = " $root
$ns = [System.Xml.Linq.XNamespace]$root.GetDefaultNamespace()
$nsUap2 = [System.Xml.Linq.XNamespace]$root.GetNamespaceOfPrefix("uap2")
$Capabilities = $xDoc.Descendants($ns + "Capabilities")
$Capability = [System.Linq.Enumerable]::First($Capabilities)

$newElement = New-Object System.Xml.Linq.XElement($nsUap2 + "Capability")
$newElement.SetAttributeValue("Name", 'perceptionSensorsExperimental')
$Capability.Add($newElement)
Write-Host $xDoc
$xDoc.Save($outputFilename)

When you use .CreateElement() to create the new node, there is a third parameter you can give it, which is the url taken from the xmlns:rescap declaration当您使用.CreateElement()创建新节点时,您可以为其提供第三个参数,即从xmlns:rescap声明中获取的 url

By using that, things should work as wanted:通过使用它,事情应该按预期工作:

# the full path and filename
$file = 'D:\Test\Package.appxmanifest'
# load the xml file. This way, you are ensured to get the file encoding correct
$xml = [System.Xml.XmlDocument]::new()
$xml.Load($file)

# use the url from the `xmlns:rescap` declaration
$nsUri   = $xml.DocumentElement.GetNamespaceOfPrefix('rescap')
$newNode = $xml.CreateElement("rescap", "Capability", $nsUri)
$newNode.SetAttribute("Name","perceptionSensorsExperimental")
[void]$xml.Package.Capabilities.AppendChild($newNode)

$xml.Save($file)

Result (shortened)结果(缩短)

. . .
  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="internetClientServer" />
    <Capability Name="privateNetworkClientServer" />
    <uap:Capability Name="sharedUserCertificates" />
    <uap2:Capability Name="spatialPerception" />
    <DeviceCapability Name="webcam" />
    <DeviceCapability Name="microphone" />
    <DeviceCapability Name="gazeinput" />
    <rescap:Capability Name="perceptionSensorsExperimental" />
  </Capabilities>
</Package>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM