本文介绍了PowerShell:附加XML元素会在同一节点上重写现有元素吗? (办公室)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我认为这不是特定于我正在使用的XML,但为了以防万一,我使用PowerShell脚本来修改Office 2010功能区的内容。  我使用类似于以下内容的代码段将控件添加到自定义组: $ OfficeUI = New-Object XML $ UIFile = $ ENV:LOCALAPPDATA + '\Microsoft\Office\olkmailitem.officeui' $ OfficeUI.Load($ UIFILE) $ NSM =新物体System.Xml.XmlNamespaceManager $ OfficeUI.NameTable $ NSM.AddNamespace('mso','http://schemas.microsoft.com/office/2009/07/customui') $ mso = $ NSM.LookupNamespace('mso') $ TAB = $ OfficeUI.customUI.ribbon.tabs.tab | ?{$ _.idQ -like'mso:TabInsert'} $ IncludeGroup = $ TAB.group | ? {$ _ ID样 'MyCustomInsertInclude'} $按钮= $ OfficeUI.CreateElement( 'MSO:控制',$ MSO) $ ATTR = $ OfficeUI.CreateAttribute( 'IDQ') $ attr.value ='mso:CalendarButton' $ QUIET = $ Button.Attributes.Append($ attr) $ attr = $ OfficeUI.CreateAttribute('visible') $ QUIET = $ Button.Attributes.Append($ attr) $ QUIET = $ IncludeGroup.AppendChild($ Button) this完全符合预期,并给我这一大块XML(只是相关部分:   < mso:tabs>    < ; MSO:标签IDQ = QUOT; MSO:TabInsert"> &NBSP;&NBSP;&NBSP;&NBSP;< MSO:组IDQ = QUOT; MSO:GroupInclude"可见= QUOT假QUOT; / > &NBSP;&NBSP;&NBSP;&NBSP;< MSO:组ID = QUOT; MyCustomInsertInclude"标签= QUOT;包含" insertBeforeQ = QUOT; MSO:GroupInsertTables" AUTOSCALE = QUOT;真"> ; $      < mso:control idQ =" mso :CalendarButton"可见= QUOT;真" />   &NBSP; &NBSP; &NBSP; < / mso:group>   &NBSP; &NBSP; < / MSO:标签> 但是,我想添加一些额外的控件: $ Button2 = $ OfficeUI。 CreateElement('mso:control',$ mso) $ attr = $ OfficeUI.CreateAttribute('idQ') $ attr.value ='mso:AttachFile' $ QUIET = $ Button2 .Attributes.Append($ ATTR) $ ATTR = $ OfficeUI.CreateAttribute( '可见') $ QUIET = $ Button2.Attributes.Append($ ATTR) $ QUIET = $ IncludeGroup .AppendChild($ Button2)这导致以下XML片段:   &NBSP; < mso:tabs>   &NBSP; &NBSP; < mso:tab idQ =" mso:TabInsert">   &NBSP; &NBSP; &NBSP; < mso:group idQ =" mso:GroupInclude"可见= QUOT假QUOT; />   &NBSP; &NBSP; &NBSP; < mso:group id =" MyCustomInsertInclude"标签= QUOT;包含" insertBeforeQ = QUOT; MSO:GroupInsertTables" autoScale =" true">   &NBSP; &NBSP; &NBSP; &NBSP; < mso:control idQ =" mso:AttachFile"可见= QUOT;真" />   &NBSP; &NBSP; &NBSP; &NBSP; < mso:control idQ =" mso:AttachFile"可见= QUOT;真" />   &NBSP; &NBSP; &NBSP; < / mso:group>   &NBSP; &NBSP; < / MSO:标签> Errrr ....嗯?  为什么会这样?  我添加的控件数量无关紧要,所有控件都添加了最后一个控件的属性。  奇怪的是,如果我编写一个SEPARATE脚本来添加下一个控件也没关系....我添加的组中的所有控件都被重写为我添加的控件。 这是基本的,我只是关于Powershell,XML或两者兼而有之吗? 解决方案 I don't think that this is specific to the XML I'm working with, but just in case, I'm using PowerShell scripts to modify the contents of Office 2010 ribbons.  I use a snippet similar to the below to add a control to a custom group:$OfficeUI= New-Object XML$UIFile=$env:localappdata + '\Microsoft\Office\olkmailitem.officeui'$OfficeUI.Load($UIFile)$NSM=New-Object System.Xml.XmlNamespaceManager $OfficeUI.NameTable$NSM.AddNamespace('mso','http://schemas.microsoft.com/office/2009/07/customui')$mso=$NSM.LookupNamespace('mso')$TAB=$OfficeUI.customUI.ribbon.tabs.tab | ?{$_.idQ -like 'mso:TabInsert'}$IncludeGroup=$TAB.group | ? {$_.id -like 'MyCustomInsertInclude'}$Button=$OfficeUI.CreateElement('mso:control',$mso)$attr=$OfficeUI.CreateAttribute('idQ')$attr.value='mso:CalendarButton'$QUIET=$Button.Attributes.Append($attr)$attr=$OfficeUI.CreateAttribute('visible')$QUIET=$Button.Attributes.Append($attr)$QUIET=$IncludeGroup.AppendChild($Button)This works exactly as expected and gives me this chunk of XML (just the relevant section:    <mso:tabs>      <mso:tab idQ="mso:TabInsert">        <mso:group idQ="mso:GroupInclude" visible="false" />        <mso:group id="MyCustomInsertInclude" label="Include" insertBeforeQ="mso:GroupInsertTables" autoScale="true">          <mso:control idQ="mso:CalendarButton" visible="true" />        </mso:group>      </mso:tab>However, I want to add some additional controls:$Button2=$OfficeUI.CreateElement('mso:control',$mso)$attr=$OfficeUI.CreateAttribute('idQ')$attr.value='mso:AttachFile'$QUIET=$Button2.Attributes.Append($attr)$attr=$OfficeUI.CreateAttribute('visible')$QUIET=$Button2.Attributes.Append($attr)$QUIET=$IncludeGroup.AppendChild($Button2)This results in the following XML snippet:    <mso:tabs>      <mso:tab idQ="mso:TabInsert">        <mso:group idQ="mso:GroupInclude" visible="false" />        <mso:group id="MyCustomInsertInclude" label="Include" insertBeforeQ="mso:GroupInsertTables" autoScale="true">          <mso:control idQ="mso:AttachFile" visible="true" />          <mso:control idQ="mso:AttachFile" visible="true" />        </mso:group>      </mso:tab>Errrr....huh?  Why did THAT happen?  It doesn't matter how many controls I add, all of them have the attributes of the last one added.  Oddly, it also doesn't matter if I write a SEPARATE script to add the next control....all of the controls in the group I add to are re-written as the control I added.Is  this something basic I just dunno about Powershell, XML, or both? 解决方案 这篇关于PowerShell:附加XML元素会在同一节点上重写现有元素吗? (办公室)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 08:02