public void MakePackageReferencePrivate(string packageName) { var xDocument = _xmlHelper.LoadFromFile(FilePath); var projectElement = xDocument.Element("Project"); if (projectElement == null) { throw new InvalidDataException($"Could not locate the 'Project' element in the file {FilePath}."); } var referenceGroups = projectElement.Elements("ItemGroup").Select( i => i.Elements("PackageReference").Where( r => { var attribute = r.Attribute("Include"); return(attribute != null && attribute.Value.Equals(packageName, StringComparison.OrdinalIgnoreCase)); })).Where(i => i.Any()).ToList(); var packageReferences = referenceGroups.FirstOrDefault()?.ToList(); if (packageReferences == null || packageReferences.Count < 1) { throw new InvalidDataException($"Could not find a package reference element for package '{packageName}' in file '{FilePath}'."); } if (referenceGroups.Count > 1 || packageReferences.Count > 1) { throw new InvalidDataException($"Found multiple package reference elements for package '{packageName}' in file '{FilePath}'."); } var packageReference = packageReferences.First(); foreach (var privateAssetElement in packageReference.Elements("PrivateAssets")) { privateAssetElement.Remove(); } packageReference.Add(new XElement("PrivateAssets", "All")); foreach (var excludeAssetsElement in packageReference.Elements("ExcludeAssets")) { excludeAssetsElement.Remove(); } packageReference.Add(new XElement("ExcludeAssets", "contentFiles")); _xmlHelper.Save(xDocument, FilePath); }