Андрей Ка
Андрей Ка

Reputation: 774

Change tag value in .csproj file with python

my idea to replace named tag UrlPublish to my needed path on .csproj file , i need to do it 10 times(path1, path2, path3...) to create 10 different msbuild deployment manifests. i have a following code:

<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>

I try to using Python and xml parser. But if i try to find a PublishUrl tag i don't get changes ... my python code is :

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

How can i parse .csproj file? its not xml and i don't understand how can i replace tag value.

Upvotes: 1

Views: 1084

Answers (1)

Igor Gatis
Igor Gatis

Reputation: 4898

The problem is with xmlns. Try:

    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)

Upvotes: 1

Related Questions