c# - 如何将文档 xml 文件构建到适当的路径?

标签 c# .net visual-studio

我想为我的 C# 项目构建文档 xml 文件。我怎样才能做到这一点?

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

我想将文档构建到下面的 OutputPath。

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

最佳答案

您需要将以下行添加到 csproject 文件中:<DocumentationFile>bin\Debug\ProjectName.XML</DocumentationFile> (在 PropertyGroup 下)

在项目属性中,您可以使用“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);
}

关于c# - 如何将文档 xml 文件构建到适当的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22843710/

相关文章:

c# - 一个类实现两个不同的 IObservables?

不可序列化对象上的 C# DoDragDrop

C# - 以字节 block 的形式从 Google Drive 下载

c# - 使用具体类型而不是接口(interface)在性能方面是否更好

c# - 尝试使用 ksp/unity3d 二进制文件时出现问题?

c# - 用户 session ID 作为 Ajax 调用中的参数

c# - 在继承类中使用 ReactiveCommand 和 WhenAny

visual-studio - 在哪里可以下载Visual Studio 2019的主题?

visual-studio - 在 Chrome 中调试 Blazor 应用程序时,新的浏览器窗口会打开并禁用扩展程序 - 希望现有窗口中的新选项卡具有扩展程序

c++ - Visual Studio 2010转换2005解决方案(C++项目)丢失项目依赖