加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用Powershell编辑XML?

发布时间:2020-12-16 07:40:52 所属栏目:百科 来源:网络整理
导读:我试图在Power shell中编辑一些xml.这是我在做什么: $path = 'C:ProjectsNuGetTestPackageNuGetTestPackage'$projName = 'NuGetTestPackage1.csproj'cd $path$file = gi $projName$pattern = 'TextTemplatingFileGenerator'$xml = [xml](gc $file)$node
我试图在Power shell中编辑一些xml.这是我在做什么:
$path = 'C:ProjectsNuGetTestPackageNuGetTestPackage'
$projName = 'NuGetTestPackage1.csproj'
cd $path
$file = gi $projName
$pattern = 'TextTemplatingFileGenerator'
$xml = [xml](gc $file)
$node = [xml] $xml.Project.ItemGroup.None.Generator | 
    Where { $_.Project.ItemGroup.None.Generator -eq $pattern } |
    Foreach {$_.Project.ItemGroup.None.Generator = ''}
$xml.Save($file.Fullname)

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>
    </ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{E750ACF1-17B5-4D83-8FC8-5A4488193099}</ProjectGuid>
    <ProjectTypeGuids>{E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>NuGetTestPackage</RootNamespace>
    <AssemblyName>NuGetTestPackage</AssemblyName>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <MvcBuildViews>false</MvcBuildViews>
    <UseIISExpress>false</UseIISExpress>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin&;/OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin&;/OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="EntityFramework,Version=4.1.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089,processorArchitecture=MSIL">
      <Private>True</Private>
      <HintPath>..packagesEntityFramework.4.1.10331.0libEntityFramework.dll</HintPath>
    </Reference>
    <Reference Include="System.Data.Entity" />
    <Reference Include="System.Web.Mvc,Version=3.0.0.0,PublicKeyToken=31bf3856ad364e35,processorArchitecture=MSIL" />
    <Reference Include="System.Web.WebPages,Version=1.0.0.0,processorArchitecture=MSIL" />
    <Reference Include="System.Web.Helpers,processorArchitecture=MSIL" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Web.DynamicData" />
    <Reference Include="System.Web.Entity" />
    <Reference Include="System.Web.ApplicationServices" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Core" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Web" />
    <Reference Include="System.Web.Extensions" />
    <Reference Include="System.Web.Abstractions" />
    <Reference Include="System.Web.Routing" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Web.Services" />
    <Reference Include="System.EnterpriseServices" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="ApplicationApplicationConfigurationSettings.cs" />
    <Compile Include="Global.asax.cs">
      <DependentUpon>Global.asax</DependentUpon>
    </Compile>
    <Compile Include="PropertiesAssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="Contentthemesbaseimagesui-bg_flat_0_aaaaaa_40x100.png" />
    <Content Include="Contentthemesbaseimagesui-bg_flat_75_ffffff_40x100.png" />
    <Content Include="Contentthemesbaseimagesui-bg_glass_55_fbf9ee_1x400.png" />
    <Content Include="Contentthemesbaseimagesui-bg_glass_65_ffffff_1x400.png" />
    <Content Include="Contentthemesbaseimagesui-bg_glass_75_dadada_1x400.png" />
    <Content Include="Contentthemesbaseimagesui-bg_glass_75_e6e6e6_1x400.png" />
    <Content Include="Contentthemesbaseimagesui-bg_glass_95_fef1ec_1x400.png" />
    <Content Include="Contentthemesbaseimagesui-bg_highlight-soft_75_cccccc_1x100.png" />
    <Content Include="Contentthemesbaseimagesui-icons_222222_256x240.png" />
    <Content Include="Contentthemesbaseimagesui-icons_2e83ff_256x240.png" />
    <Content Include="Contentthemesbaseimagesui-icons_454545_256x240.png" />
    <Content Include="Contentthemesbaseimagesui-icons_888888_256x240.png" />
    <Content Include="Contentthemesbaseimagesui-icons_cd0a0a_256x240.png" />
    <Content Include="Contentthemesbasejquery.ui.accordion.css" />
    <Content Include="Contentthemesbasejquery.ui.all.css" />
    <Content Include="Contentthemesbasejquery.ui.autocomplete.css" />
    <Content Include="Contentthemesbasejquery.ui.base.css" />
    <Content Include="Contentthemesbasejquery.ui.button.css" />
    <Content Include="Contentthemesbasejquery.ui.core.css" />
    <Content Include="Contentthemesbasejquery.ui.datepicker.css" />
    <Content Include="Contentthemesbasejquery.ui.dialog.css" />
    <Content Include="Contentthemesbasejquery.ui.progressbar.css" />
    <Content Include="Contentthemesbasejquery.ui.resizable.css" />
    <Content Include="Contentthemesbasejquery.ui.selectable.css" />
    <Content Include="Contentthemesbasejquery.ui.slider.css" />
    <Content Include="Contentthemesbasejquery.ui.tabs.css" />
    <Content Include="Contentthemesbasejquery.ui.theme.css" />
    <Content Include="Global.asax" />
    <Content Include="ContentSite.css" />
    <Content Include="Scriptsjquery-1.5.1-vsdoc.js" />
    <Content Include="Scriptsjquery-1.5.1.js" />
    <Content Include="Scriptsjquery-1.5.1.min.js" />
    <Content Include="Scriptsjquery-ui-1.8.11.js" />
    <Content Include="Scriptsjquery-ui-1.8.11.min.js" />
    <Content Include="Scriptsjquery.validate-vsdoc.js" />
    <Content Include="Scriptsjquery.validate.js" />
    <Content Include="Scriptsjquery.validate.min.js" />
    <Content Include="Scriptsmodernizr-1.7.js" />
    <Content Include="Scriptsmodernizr-1.7.min.js" />
    <Content Include="T4WebConfigSettingGeneratorScript.txt">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>WebConfigSettingGeneratorScript.tt</DependentUpon>
    </Content>
    <Content Include="T4WebConfigSettingGeneratorScript1.txt" />
    <Content Include="Web.config" />
    <Content Include="Web.Debug.config">
      <DependentUpon>Web.config</DependentUpon>
    </Content>
    <Content Include="Web.Release.config">
      <DependentUpon>Web.config</DependentUpon>
    </Content>
    <Content Include="Scriptsjquery.unobtrusive-ajax.js" />
    <Content Include="Scriptsjquery.unobtrusive-ajax.min.js" />
    <Content Include="Scriptsjquery.validate.unobtrusive.js" />
    <Content Include="Scriptsjquery.validate.unobtrusive.min.js" />
    <Content Include="ScriptsMicrosoftAjax.js" />
    <Content Include="ScriptsMicrosoftAjax.debug.js" />
    <Content Include="ScriptsMicrosoftMvcAjax.js" />
    <Content Include="ScriptsMicrosoftMvcAjax.debug.js" />
    <Content Include="ScriptsMicrosoftMvcValidation.js" />
    <Content Include="ScriptsMicrosoftMvcValidation.debug.js" />
    <Content Include="ViewsWeb.config" />
    <Content Include="Views_ViewStart.cshtml" />
    <Content Include="ViewsSharedError.cshtml" />
    <Content Include="ViewsShared_Layout.cshtml" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Controllers" />
    <Folder Include="Models" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="packages.config" />
  </ItemGroup>
  <ItemGroup>
    <None Include="CodeTemplatesAddControllerController.tt" />
    <None Include="CodeTemplatesAddControllerControllerWithContext.tt" />
    <None Include="CodeTemplatesAddViewCSHTMLCreate.tt" />
    <None Include="CodeTemplatesAddViewCSHTMLDelete.tt" />
    <None Include="CodeTemplatesAddViewCSHTMLDetails.tt" />
    <None Include="CodeTemplatesAddViewCSHTMLEdit.tt" />
    <None Include="CodeTemplatesAddViewCSHTMLEmpty.tt" />
    <None Include="CodeTemplatesAddViewCSHTMLList.tt" />
    <None Include="T4GeneratorsWebConfigGenerator.tt" />
    <None Include="T4TemplatesWebConfigTemplate.tt" />
    <None Include="T4WebConfigSettingGeneratorScript.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>WebConfigSettingGeneratorScript.txt</LastGenOutput>
    </None>
    <None Include="T4WebConfigSettingGeneratorScript.tt.log">
      <DependentUpon>WebConfigSettingGeneratorScript.tt</DependentUpon>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
  </ItemGroup>
  <Import Project="$(MSBuildBinPath)Microsoft.CSharp.targets" />
  <Import Project="$(MSBuildExtensionsPath32)MicrosoftVisualStudiov10.0WebApplicationsMicrosoft.WebApplication.targets" />
  <!-- To modify your build process,add your task inside one of the targets below and uncomment it. 
   Other similar extension points exist,see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target> -->
  <Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
  </Target>
  <ProjectExtensions>
    <VisualStudio>
      <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
        <WebProjectProperties>
          <UseIIS>False</UseIIS>
          <AutoAssignPort>True</AutoAssignPort>
          <DevelopmentServerPort>55059</DevelopmentServerPort>
          <DevelopmentServerVPath>/</DevelopmentServerVPath>
          <IISUrl>
          </IISUrl>
          <NTLMAuthentication>False</NTLMAuthentication>
          <UseCustomServer>False</UseCustomServer>
          <CustomServerUrl>
          </CustomServerUrl>
          <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
        </WebProjectProperties>
      </FlavorProperties>
    </VisualStudio>
  </ProjectExtensions>
</Project>

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读