Search code examples
c#xmlnuget

Configuring a NuGet package that copies files into the build directory of the referencing project


I have a collection of .NET framework 4.8 projects that all contain the same set of XSD files for XML validation. I've created a very basic XML validation NuGet package in .netstandard 2.0 that contains these XSDs in one single location so they don't get out of sync. Included in the .netstandard 2.0 project is a Xsds folder with multiple XSD files inside with "CopyAlways".

Below is a simple way to access the XmlUtils NuGet package from another project.

XmlType xmlType = XmlType.SpecificFile;
XmlDocument xmlFile = new XmlDocument();
xmlFile.Load(File.ReadAllText("PathToXmlFile.xml"));
List<string> errors = XmlUtils.ValidateXmlFile(xmlType, xmlFile);

The above works as expected, however I've done a few hours of research and haven't found a way to get a project that imports this NuGet package to create the Xsds folder with the XSD files inside.

I have successfully been able to have the NuGet package create the Xsds folder at the root level of the importing project, but I explicitly do not want to do that. I just want the XmlUtils class to be available (which I've already done), and the Xsds folder to be built whenever the referencing project builds.

My csproj has the following currently:

<ItemGroup>
  <None Include="Xsds\*.xsd" Pack="True" PackagePath="Xsds\">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
</ItemGroup>

Solution

  • Found the solution this morning.

    1. I converted my packages.config to package reference See docs
    2. I updated my csproj of the NuGet package to be:
    <None Include="Xsds\*.xsd">
      <pack>true</pack>
      <PackagePath>contentFiles\any\any\Xsds\</PackagePath>
      <PackageCopyToOutput>true</PackageCopyToOutput>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    

    I would like to know if this is possible to do with a packages.config. It's rather annoying that this NuGet package simply won't work unless the user is doing it a specific way..