Click here to Skip to main content
15,900,461 members
Articles / Programming Languages / XML

Programmatically Determine Nuget Dependencies for a Project

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
17 Nov 2017CPOL 5.4K   1
How to programmatically determine Nuget Dependencies for a project

I like to use Nuget pretty extensively. It is great for distributing open source libraries to the public, but it is also a great tool for managing your own code libraries, and sharing them across projects.

There are times when I cannot simply run the NuGet utility on a single project file. Sometimes, I need to create my own NuSpec file, for example if I am including the build outputs of multiple projects, and generate a package from that. At that point, it becomes important to know the landscape of your NuGet dependencies in your projects, so you can properly reference them in a NuSpec file.

The solution is really pretty simple — each project’s dependencies are stored in a file named packages.config at the root directory of the project. This configuration file contains an XML document that lists your each dependency’s ID, version, and target framework.

XML
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Onion.SolutionParser.Parser" 
  version="1.0.0.0" targetFramework="net45" />
</packages>

So all you need to do is deserialize the XML to an object, and you can catalog your dependencies, write them into a NuSpec file, or do whatever else you need to do with them.

C#
public class NugetPackageConfigItem
{
  [XmlAttribute("id")]
  public string Id { get; set; }

  [XmlAttribute("version")]
  public string Version { get; set; }

  [XmlAttribute("targetFramework")]
  public string TargetFramework { get; set; }
}

[XmlRoot("packages")]
public class NugetPackageConfigCollection
{
  [XmlElement("package")]
  public NugetPackageConfigItem[] Packages { get; set; }

  public static NugetPackageConfigCollection Deserialize(string path)
  {
    using (var stream = File.OpenRead(path))
    {
      var serializer = new XmlSerializer(typeof(NugetPackageConfigCollection));
      var collection = serializer.Deserialize(stream) as NugetPackageConfigCollection;
      return collection;
    }
  }
}

Hope this tip was helpful and non-obvious. Happy coding.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I have been slinging code for over 15 years, and have particular interests in server programming, build and test automation, and deployment.

Comments and Discussions

 
QuestionNon-Obvious Pin
Dirk Bahle18-Nov-17 0:14
Dirk Bahle18-Nov-17 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.