如何将文档 xml 文件构建到适当的路径

本文关键字:路径 构建 文件 文档 xml | 更新日期: 2023-09-27 17:56:32

我想为我的 C# 项目构建文档 xml 文件。我该怎么做?

这是我CommonBase.props文件,由大约一百个csproj文件导入。它的目的是在不同的地方保存编辑相同的信息。

我想将文档构建到下面的输出路径。

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

这就是csproj的样子

<?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" />

如何将文档 xml 文件构建到适当的路径

您需要

将以下行添加到 csproject 文件中: <DocumentationFile>bin'Debug'ProjectName.XML</DocumentationFile>(在属性组下)

在"项目属性"中,您可以使用"XML 文档文件"复选框手动将其放入"生成"选项卡中。

或者使用代码一次更改多个项目文件:

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);
}

我可以使用模板编写文件路径:

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DocumentationFile>$(MSBuildThisFileDirectory)'bin'$(Configuration)'$(TargetFramework)'<File_Name>.xml</DocumentationFile>
  </PropertyGroup>