使用python更改.csproj文件中的标记值

时间:2017-11-26 17:10:05

标签: python xml visual-studio csproj

我想将命名标记 UrlPublish 替换为.csproj文件中我需要的路径,我需要做10次(path1,path2,path3 ...)来创建10个不同的msbuild部署清单。我有以下代码:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0" DefaultTargets="Build">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
  <!--This section defines project-level properties.AssemblyNameName of the output assembly.ConfigurationSpecifies a default value for debug.OutputTypeMust be "Library" for VSTO.PlatformSpecifies what CPU the output of this project can run on.NoStandardLibrariesSet to "false" for VSTO.RootNamespaceIn C#, this specifies the namespace given to new files. In VB, all objects arewrapped in this namespace at runtime.-->
  <PropertyGroup>
    <ProjectTypeGuids>{asdasdd-18E2-41B9-852F-Fsdsdsd0CAA33};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{xxxxxxx-6155-4C9E-91A0-xxxxxx552}</ProjectGuid>
    <OutputType>Library</OutputType>
    <NoStandardLibraries>false</NoStandardLibraries>
    <RootNamespace>Com.project.Distribution.Mvp</RootNamespace>
    <AssemblyName>Com.project.Distribution.Mvp</AssemblyName>
    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
    <DefineConstants>VSTO40</DefineConstants>
    <IsWebBootstrapper>False</IsWebBootstrapper>
    <FileUpgradeFlags/>
    <UpgradeBackupLocation/>
    <OldToolsVersion>15.0</OldToolsVersion>
    <VSTO_TrustAssembliesLocation>true</VSTO_TrustAssembliesLocation>
    <BootstrapperEnabled>true</BootstrapperEnabled>
    <PublishUrl>path</PublishUrl>
    <InstallUrl/>
    <TargetCulture>en</TargetCulture>
    <ApplicationVersion>1.0.1.8</ApplicationVersion>
    <AutoIncrementApplicationRevision>true</AutoIncrementApplicationRevision>
    <UpdateEnabled>true</UpdateEnabled>
    <UpdateInterval>2</UpdateInterval>
    <UpdateIntervalUnits>hours</UpdateIntervalUnits>
    <ProductName>Com.project.Distribution.Mvp</ProductName>
    <PublisherName/>
    <SupportUrl/>
    <FriendlyName>Com.project.Distribution.Mvp</FriendlyName>
    <OfficeApplicationDescription/>
    <LoadBehavior>3</LoadBehavior>
  </PropertyGroup>
  <ItemGroup>
    <BootstrapperPackage Include=".NETFramework,Version=v4.5.2">
      <Visible>False</Visible>
      <ProductName>Microsoft .NET Framework 4.5.2 %28x86 and x64%29</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
      <Visible>False</Visible>
      <ProductName>.NET Framework 3.5 SP1</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Office.PIARedist.2007">
      <Visible>False</Visible>
      <ProductName>Microsoft Office 2007 Primary Interop Assemblies</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.VSTORuntime.4.0">
      <Visible>False</Visible>
      <ProductName>Microsoft Visual Studio 2010 Tools for Office Runtime %28x86 and x64%29</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
      <Visible>False</Visible>
      <ProductName>Windows Installer 3.1</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
  </ItemGroup>
  <PropertyGroup>

我尝试使用Python和xml解析器。但是如果我试图找到一个PublishUrl标签,我就不会有变化...我的python代码是:

   import xml.etree.ElementTree as ET
    tree = ET.parse('file.csproj')
    root = tree.getroot()
    elem = tree.findall(".//PublishUrl")
    print(elem)

我如何解析.csproj文件?它不是xml,我不明白如何替换标签值。

1 个答案:

答案 0 :(得分:0)

问题出在xmlns上。试试:

    import xml.etree.ElementTree as ET
    tree = ET.parse('file.csproj')
    root = tree.getroot()
    elem = tree.findall(".//{http://schemas.microsoft.com/developer/msbuild/2003}PublishUrl")
    print(elem)