How do you include Xml Docs for a class library in a NuGet package?

C#NugetNuget Package

C# Problem Overview


I am creating a NuGet package for a C# class library, and I would like to include generated Xml Documentation with the library. This is my nuspec file:

<?xml version="1.0" encoding="utf-8"?>
<package>
  <metadata>
    <id>MyLibrary</id>
    <version>1.0.0.0</version>
    <authors>John Nelson</authors>
    <language>en-US</language>
    <description>A C# class library</description>
  </metadata>
  <files>
    <file src="..\..\build\MyLibrary.dll" target="lib\Net40" />
    <file src="..\..\build\MyLibrary.xml" target="lib\Net40" />
  </files>
</package>

When I build the package with this command:

nuget pack MyLibrary.nuspec

It generates an error. If I remove the line:

<file src="..\..\build\MyLibrary.xml" target="lib\Net40" />

NuGet.exe successfully creates the nupkg. I can even unzip the package, and verify that the contents are correct. What am I doing wrong? Should the xml file go into a different target directory?

C# Solutions


Solution 1 - C#

The problem was that I didn't check "Generate Xml Documentation" for the build configuration I was using. That nuspec is correct.

enter image description here

Solution 2 - C#

In .NET Core/Standard you can do this by editing the project XML file, for example:

<PropertyGroup>
	<TargetFramework>netstandard2.0</TargetFramework>
	<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup>
	<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>

This will output the documentation as an XML file next to your output assembly.

EDIT: As a side note once you enable GenerateDocumentationFile you will probably get lots of warnings on your public methods for not having added full documentation tags. If you want to disable these warnings simply add in the PropertyGroup:

<NoWarn>$(NoWarn);1591</NoWarn>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn NelsonView Question on Stackoverflow
Solution 1 - C#John NelsonView Answer on Stackoverflow
Solution 2 - C#bytedevView Answer on Stackoverflow