3

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)

3
  • What project type is ist? do you use a csproj with integrated NuGet (VS 2017 "sdk-based" csproj) or nuspec to pack? Commented Feb 7, 2018 at 13:54
  • @001, Would you please share the latest information about this issue? Commented Feb 21, 2018 at 8:04
  • I manually copied the XML file to the correct directory, because I couldnt get it to work. :( Commented Feb 25, 2018 at 8:53

1 Answer 1

3

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:

enter image description here

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:

enter image description here

After build, the nuget package generated at output folder:

enter image description here

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your response leo, I am using .NET Core 2, is it the same?
@001, yes, you just need use netcoreapp2.0 in the .nuspec file, for the details, please check my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.