I have two projects, MyModels (contains class models) and MyExample The XML file is created in MyModels project, thats fine.
Now in MyExample project, I reference MyModels by installing it via nuget, but how do I include the XML document in the package when the nuget package is installed? (its always missing)
Xml documentation not packed in class library
If you are in the common .csproj type project not "SDK-based", you can include the XML document directly to include in the package by using the <files> node in the .nuspec file, which follows the <metadata> tag:
<files>
<file src="bin\Debug\MyModels.dll" target="lib\Net45" />
<file src="bin\Debug\MyModels.xml" target="lib\Net45" />
</files>
Following is my .nuspec:
<?xml version="1.0"?>
<package >
<metadata>
<id>MyModels</id>
<version>1.0.0</version>
<authors>Tester</authors>
<owners>Tester</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2018</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
<files>
<file src="bin\Debug\MyModels.dll" target="lib\Net45" />
<file src="bin\Debug\MyModels.xml" target="lib\Net45" />
</files>
</package>
Then build the package with this command:
nuget pack MyModels.nuspec
NuGet.exe successfully creates the nupkg:

If you are using the new .csproj type project (VS2017 SDK-based), you can set GenerateDocumentationFile into your csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
</Project>
With the option GenerateDocumentationFile, MSBuild will include all the generate file in the nuget package.
Besides, if you want the XML document to the project of MyExample, you can change the target to the content:
<files>
<file src="bin\Debug\MyModels.dll" target="lib\Net45" />
<file src="bin\Debug\MyModels.xml" target="Content\MyModels.xml" />
</files>
Hope this helps.
Update for comment:
I am using .NET Core 2, is it the same?
Yes, you just need update the src and target in the .nuspec file:
<files>
<file src="bin\Debug\netcoreapp2.0\MyModels.dll" target="lib\netcoreapp2.0" />
<file src="bin\Debug\netcoreapp2.0\MyModels.xml" target="lib\netcoreapp2.0" />
</files>
Or you can directly use the new .csproj type project (VS2017 SDK-based), set GenerateDocumentationFile into .csproj file, then check the checkbox "Generate NuGet Package on build" on the Package tab of properties of project:

After build, the nuget package generated at output folder:

Note: if you want add want the XML document to the project of MyExample, you should use content files:
https://learn.microsoft.com/en-us/nuget/reference/nuspec#including-assembly-files
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With