8

I want to build documentation xml files for my C# projects. How can I do that?

This is my CommonBase.props file, imported by about a hundred csproj files. The point of it is to save editing the same information in different places.

I want to build the documentation to the OutputPath below.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Common properties for  projects. This project can't be built by itself, but is imported by several projects. Be careful to prepend $(MSBuildThisFileDirectory) before any paths relative to this file.

    If you import this project from a csproj file, you should still define at least ProjectGuid, AssemblyName, RootNamespace and OutputType in the csproj.  
  -->
  <PropertyGroup>
    <!-- If configuration not specified, default to debug. If platform not specified, default to x64. Necessary for London continuous integration server. -->
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x64</Platform>

    <PlatformTarget>$(Platform)</PlatformTarget>
    <ProductVersion>9.0.30729</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <DebugSymbols>true</DebugSymbols>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Platform)' == 'x86'">
    <OutputPath>$(MSBuildThisFileDirectory)..\..\bin32\</OutputPath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Platform)' == 'x64'">
    <OutputPath>$(MSBuildThisFileDirectory)..\..\bin\</OutputPath>
  </PropertyGroup>
</Project>

This is what a csproj looks like

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <Import Project="..\Build\CommonBase.props" />
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465

2 Answers2

11

You need to add the following line to the csproject files: <DocumentationFile>bin\Debug\ProjectName.XML</DocumentationFile> (under PropertyGroup)

In the Project Properties you can manually put this in the Build tab with the "XML Documentation File" checkbox.

Or with code to change many project files at once:

var projectFiles = System.IO.Directory.GetFiles(
    @"C:\somePath", "*.csproj", SearchOption.AllDirectories);

foreach (var file in projectFiles)
{
    var xmlFile = XDocument.Load(file);
    var propNode = xmlFile.Root.Elements().First();
    var assemblyName = propNode.Elements().First(x =>x.Name.LocalName == "AssemblyName").Value;
    propNode.Add(new XElement("DocumentationFile", string.Format("somePlace\\{0}.XML", assemblyName)));
    xmlFile.Save(file);
}
Laoujin
  • 9,962
  • 7
  • 42
  • 69
  • 4
    I recommend using `bin\$(Configuration)\YourProjectName.xml` in the initial `` to avoid having to repeat it for each build configuration. – Dai Nov 26 '18 at 01:29
11

I can compose the file path using the template:

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DocumentationFile>$(MSBuildThisFileDirectory)\bin\$(Configuration)\$(TargetFramework)\<File_Name>.xml</DocumentationFile>
  </PropertyGroup>
Gilberto Alexandre
  • 2,227
  • 1
  • 18
  • 20