如何使用Powershell编辑XML?

前端之家收集整理的这篇文章主要介绍了如何使用Powershell编辑XML?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在Power shell中编辑一些xml.这是我在做什么:
  1. $path = 'C:\Projects\NuGetTestPackage\NuGetTestPackage'
  2. $projName = 'NuGetTestPackage1.csproj'
  3. cd $path
  4. $file = gi $projName
  5. $pattern = 'TextTemplatingFileGenerator'
  6. $xml = [xml](gc $file)
  7. $node = [xml] $xml.Project.ItemGroup.None.Generator |
  8. Where { $_.Project.ItemGroup.None.Generator -eq $pattern } |
  9. Foreach {$_.Project.ItemGroup.None.Generator = ''}
  10. $xml.Save($file.Fullname)

这是XML:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3. <PropertyGroup>
  4. <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
  5. <Platform Condition=" '$(Platform)' == '' ">Anycpu</Platform>
  6. <ProductVersion>
  7. </ProductVersion>
  8. <SchemaVersion>2.0</SchemaVersion>
  9. <ProjectGuid>{E750ACF1-17B5-4D83-8FC8-5A4488193099}</ProjectGuid>
  10. <ProjectTypeGuids>{E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
  11. <OutputType>Library</OutputType>
  12. <AppDesignerFolder>Properties</AppDesignerFolder>
  13. <RootNamespace>NuGetTestPackage</RootNamespace>
  14. <AssemblyName>NuGetTestPackage</AssemblyName>
  15. <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
  16. <MvcBuildViews>false</MvcBuildViews>
  17. <UseIISExpress>false</UseIISExpress>
  18. </PropertyGroup>
  19. <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|Anycpu' ">
  20. <DebugSymbols>true</DebugSymbols>
  21. <DebugType>full</DebugType>
  22. <Optimize>false</Optimize>
  23. <OutputPath>bin\</OutputPath>
  24. <DefineConstants>DEBUG;TRACE</DefineConstants>
  25. <ErrorReport>prompt</ErrorReport>
  26. <WarningLevel>4</WarningLevel>
  27. </PropertyGroup>
  28. <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|Anycpu' ">
  29. <DebugType>pdbonly</DebugType>
  30. <Optimize>true</Optimize>
  31. <OutputPath>bin\</OutputPath>
  32. <DefineConstants>TRACE</DefineConstants>
  33. <ErrorReport>prompt</ErrorReport>
  34. <WarningLevel>4</WarningLevel>
  35. </PropertyGroup>
  36. <ItemGroup>
  37. <Reference Include="EntityFramework,Version=4.1.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089,processorArchitecture=MSIL">
  38. <Private>True</Private>
  39. <HintPath>..\packages\EntityFramework.4.1.10331.0\lib\EntityFramework.dll</HintPath>
  40. </Reference>
  41. <Reference Include="System.Data.Entity" />
  42. <Reference Include="System.Web.Mvc,Version=3.0.0.0,PublicKeyToken=31bf3856ad364e35,processorArchitecture=MSIL" />
  43. <Reference Include="System.Web.WebPages,Version=1.0.0.0,processorArchitecture=MSIL" />
  44. <Reference Include="System.Web.Helpers,processorArchitecture=MSIL" />
  45. <Reference Include="Microsoft.CSharp" />
  46. <Reference Include="System" />
  47. <Reference Include="System.Data" />
  48. <Reference Include="System.Drawing" />
  49. <Reference Include="System.Web.DynamicData" />
  50. <Reference Include="System.Web.Entity" />
  51. <Reference Include="System.Web.ApplicationServices" />
  52. <Reference Include="System.ComponentModel.DataAnnotations" />
  53. <Reference Include="System.Core" />
  54. <Reference Include="System.Data.DataSetExtensions" />
  55. <Reference Include="System.Xml.Linq" />
  56. <Reference Include="System.Web" />
  57. <Reference Include="System.Web.Extensions" />
  58. <Reference Include="System.Web.Abstractions" />
  59. <Reference Include="System.Web.Routing" />
  60. <Reference Include="System.Xml" />
  61. <Reference Include="System.Configuration" />
  62. <Reference Include="System.Web.Services" />
  63. <Reference Include="System.EnterpriseServices" />
  64. </ItemGroup>
  65. <ItemGroup>
  66. <Compile Include="Application\ApplicationConfigurationSettings.cs" />
  67. <Compile Include="Global.asax.cs">
  68. <DependentUpon>Global.asax</DependentUpon>
  69. </Compile>
  70. <Compile Include="Properties\AssemblyInfo.cs" />
  71. </ItemGroup>
  72. <ItemGroup>
  73. <Content Include="Content\themes\base\images\ui-bg_flat_0_aaaaaa_40x100.png" />
  74. <Content Include="Content\themes\base\images\ui-bg_flat_75_ffffff_40x100.png" />
  75. <Content Include="Content\themes\base\images\ui-bg_glass_55_fbf9ee_1x400.png" />
  76. <Content Include="Content\themes\base\images\ui-bg_glass_65_ffffff_1x400.png" />
  77. <Content Include="Content\themes\base\images\ui-bg_glass_75_dadada_1x400.png" />
  78. <Content Include="Content\themes\base\images\ui-bg_glass_75_e6e6e6_1x400.png" />
  79. <Content Include="Content\themes\base\images\ui-bg_glass_95_fef1ec_1x400.png" />
  80. <Content Include="Content\themes\base\images\ui-bg_highlight-soft_75_cccccc_1x100.png" />
  81. <Content Include="Content\themes\base\images\ui-icons_222222_256x240.png" />
  82. <Content Include="Content\themes\base\images\ui-icons_2e83ff_256x240.png" />
  83. <Content Include="Content\themes\base\images\ui-icons_454545_256x240.png" />
  84. <Content Include="Content\themes\base\images\ui-icons_888888_256x240.png" />
  85. <Content Include="Content\themes\base\images\ui-icons_cd0a0a_256x240.png" />
  86. <Content Include="Content\themes\base\jquery.ui.accordion.css" />
  87. <Content Include="Content\themes\base\jquery.ui.all.css" />
  88. <Content Include="Content\themes\base\jquery.ui.autocomplete.css" />
  89. <Content Include="Content\themes\base\jquery.ui.base.css" />
  90. <Content Include="Content\themes\base\jquery.ui.button.css" />
  91. <Content Include="Content\themes\base\jquery.ui.core.css" />
  92. <Content Include="Content\themes\base\jquery.ui.datepicker.css" />
  93. <Content Include="Content\themes\base\jquery.ui.dialog.css" />
  94. <Content Include="Content\themes\base\jquery.ui.progressbar.css" />
  95. <Content Include="Content\themes\base\jquery.ui.resizable.css" />
  96. <Content Include="Content\themes\base\jquery.ui.selectable.css" />
  97. <Content Include="Content\themes\base\jquery.ui.slider.css" />
  98. <Content Include="Content\themes\base\jquery.ui.tabs.css" />
  99. <Content Include="Content\themes\base\jquery.ui.theme.css" />
  100. <Content Include="Global.asax" />
  101. <Content Include="Content\Site.css" />
  102. <Content Include="Scripts\jquery-1.5.1-vsdoc.js" />
  103. <Content Include="Scripts\jquery-1.5.1.js" />
  104. <Content Include="Scripts\jquery-1.5.1.min.js" />
  105. <Content Include="Scripts\jquery-ui-1.8.11.js" />
  106. <Content Include="Scripts\jquery-ui-1.8.11.min.js" />
  107. <Content Include="Scripts\jquery.validate-vsdoc.js" />
  108. <Content Include="Scripts\jquery.validate.js" />
  109. <Content Include="Scripts\jquery.validate.min.js" />
  110. <Content Include="Scripts\modernizr-1.7.js" />
  111. <Content Include="Scripts\modernizr-1.7.min.js" />
  112. <Content Include="T4\WebConfigSettingGeneratorScript.txt">
  113. <AutoGen>True</AutoGen>
  114. <DesignTime>True</DesignTime>
  115. <DependentUpon>WebConfigSettingGeneratorScript.tt</DependentUpon>
  116. </Content>
  117. <Content Include="T4\WebConfigSettingGeneratorScript1.txt" />
  118. <Content Include="Web.config" />
  119. <Content Include="Web.Debug.config">
  120. <DependentUpon>Web.config</DependentUpon>
  121. </Content>
  122. <Content Include="Web.Release.config">
  123. <DependentUpon>Web.config</DependentUpon>
  124. </Content>
  125. <Content Include="Scripts\jquery.unobtrusive-ajax.js" />
  126. <Content Include="Scripts\jquery.unobtrusive-ajax.min.js" />
  127. <Content Include="Scripts\jquery.validate.unobtrusive.js" />
  128. <Content Include="Scripts\jquery.validate.unobtrusive.min.js" />
  129. <Content Include="Scripts\MicrosoftAjax.js" />
  130. <Content Include="Scripts\MicrosoftAjax.debug.js" />
  131. <Content Include="Scripts\MicrosoftMvcAjax.js" />
  132. <Content Include="Scripts\MicrosoftMvcAjax.debug.js" />
  133. <Content Include="Scripts\MicrosoftMvcValidation.js" />
  134. <Content Include="Scripts\MicrosoftMvcValidation.debug.js" />
  135. <Content Include="Views\Web.config" />
  136. <Content Include="Views\_ViewStart.cshtml" />
  137. <Content Include="Views\Shared\Error.cshtml" />
  138. <Content Include="Views\Shared\_Layout.cshtml" />
  139. </ItemGroup>
  140. <ItemGroup>
  141. <Folder Include="Controllers\" />
  142. <Folder Include="Models\" />
  143. </ItemGroup>
  144. <ItemGroup>
  145. <Content Include="packages.config" />
  146. </ItemGroup>
  147. <ItemGroup>
  148. <None Include="CodeTemplates\AddController\Controller.tt" />
  149. <None Include="CodeTemplates\AddController\ControllerWithContext.tt" />
  150. <None Include="CodeTemplates\AddView\CSHTML\Create.tt" />
  151. <None Include="CodeTemplates\AddView\CSHTML\Delete.tt" />
  152. <None Include="CodeTemplates\AddView\CSHTML\Details.tt" />
  153. <None Include="CodeTemplates\AddView\CSHTML\Edit.tt" />
  154. <None Include="CodeTemplates\AddView\CSHTML\Empty.tt" />
  155. <None Include="CodeTemplates\AddView\CSHTML\List.tt" />
  156. <None Include="T4\Generators\WebConfigGenerator.tt" />
  157. <None Include="T4\Templates\WebConfigTemplate.tt" />
  158. <None Include="T4\WebConfigSettingGeneratorScript.tt">
  159. <Generator>TextTemplatingFileGenerator</Generator>
  160. <LastGenOutput>WebConfigSettingGeneratorScript.txt</LastGenOutput>
  161. </None>
  162. <None Include="T4\WebConfigSettingGeneratorScript.tt.log">
  163. <DependentUpon>WebConfigSettingGeneratorScript.tt</DependentUpon>
  164. </None>
  165. </ItemGroup>
  166. <ItemGroup>
  167. <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
  168. </ItemGroup>
  169. <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  170. <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
  171. <!-- To modify your build process,add your task inside one of the targets below and uncomment it.
  172. Other similar extension points exist,see Microsoft.Common.targets.
  173. <Target Name="BeforeBuild">
  174. </Target>
  175. <Target Name="AfterBuild">
  176. </Target> -->
  177. <Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  178. <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
  179. </Target>
  180. <ProjectExtensions>
  181. <VisualStudio>
  182. <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
  183. <WebProjectProperties>
  184. <UseIIS>False</UseIIS>
  185. <AutoAssignPort>True</AutoAssignPort>
  186. <DevelopmentServerPort>55059</DevelopmentServerPort>
  187. <DevelopmentServerVPath>/</DevelopmentServerVPath>
  188. <IISUrl>
  189. </IISUrl>
  190. <NTLMAuthentication>False</NTLMAuthentication>
  191. <UseCustomServer>False</UseCustomServer>
  192. <CustomServerUrl>
  193. </CustomServerUrl>
  194. <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
  195. </WebProjectProperties>
  196. </FlavorProperties>
  197. </VisualStudio>
  198. </ProjectExtensions>
  199. </Project>

这个想法是清除Generator节点的值.我似乎无法让这段代码工作.我正在访问xml属性吗?看起来我每个都有一个值,但我无法正确访问它.

好的,谢谢@Keith Hill.这是答案:

  1. $path = 'C:\Projects\NuGetTestPackage\NuGetTestPackage'
  2. $ns = @{msb = 'http://schemas.microsoft.com/developer/msbuild/2003'}
  3. cd $path
  4. $results = 0
  5. $projName = 'NuGetTestPackage1.csproj'
  6. $file = gi $projName
  7. $xml = [xml](gc $projName)
  8. $xml | Select-Xml "//msb:Generator" -Namespace $ns |
  9. Foreach {
  10. $_.Node.set_InnerText('')
  11. $results = 1
  12. }
  13. if($results -eq 1){
  14. $xml.Save($file.FullName)
  15. }
这是因为您有多个“ItemGroup”元素和多个“None”元素.此时,ItemGroup是一个集合,没有“.None”属性.你最好使用XPath IMO,例如:
  1. $ns = @{msb = 'http://schemas.microsoft.com/developer/msbuild/2003'}
  2. $pattern = 'TextTemplatingFileGenerator'
  3. $xml = [xml](gc C:\temp\test.csproj)
  4. $xml | Select-Xml "//msb:Generator[text() = $pattern]" -Namespace $ns |
  5. Foreach {$_.Node.set_InnerText('')}
  6. $xml.Save('C:\temp\test.csproj')

猜你在找的XML相关文章