143

not a showstopper but when using nuget in a project, it creates a packages.config file with this shape

<?xml version="1.0" encoding="utf-8"?>
<packages>
   ... your packages
</packages> 

this gives a warning in VS

The 'packages' element is not declared.

The origin of the problem got something to do with the xml declaration I guess.

Also I think that the default definition package shouldn't throw warnings.

Does anyone know what should I change it to so I don't get this warning? (ie even if I can see it only when the file is open, it also shows as a warning constantly with certain CA rules on.)

Joel
  • 6,735
  • 4
  • 46
  • 54
roundcrisis
  • 16,035
  • 14
  • 55
  • 91

8 Answers8

187

You will see it only when the file is open. When you'll close the file in Visual Studio the warnings goes away

http://nuget.codeplex.com/discussions/261638

Tzvi Gregory Kaidanov
  • 2,866
  • 1
  • 20
  • 29
115

Actually the correct answer to this is to just add the schema to your document, like so

<packages xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">

...and you're done :)

If the XSD is not already cached and unavailable, you can add it as follows from the NuGet console

Install-Package NuGet.Manifest.Schema -Version 2.0.0

Once this is done, as noted in a comment below, you may want to move it from your current folder to the official schema folder that is found in

%VisualStudioPath%\Xml\Schemas
Stefan Z Camilleri
  • 3,856
  • 1
  • 30
  • 41
  • How come I can't go to that URL? – andrewb Dec 29 '13 at 22:10
  • 4
    You are correct in saying that the xsd in question is not accessible currently, that said, by their W3C definition, as misleading as it might be, the xmlns (namespace) declaration is nothing more than a string in the form of a URI, and it is not required, nor suggested, that a resource be actually made available there (http://en.wikipedia.org/wiki/XML_namespace) – Stefan Z Camilleri Dec 30 '13 at 00:39
  • Interesting. Isn't there a resource somewhere though that specifies how that XML file should be? How does Visual Studio check the format? – andrewb Dec 30 '13 at 03:31
  • 1
    Yes, most definitely, it is the xsd itself. Visual Studio doesn't really validate the schema, it is the library/assembly that is consuming the XML document that will do the validation.The only reason VS needs this information is to provide intellisense. When the resource cannot be found, VS might be using a pre-cached instance of the xsd, that can be found in %VsInstallDir%\xml\Schemas – Stefan Z Camilleri Dec 30 '13 at 04:01
  • 4
    So now I'm getting 13 Info messages ("Could not find schema info...") instead of just one Warning message... kind-of feels like a step back :-) – Riegardt Steyn Jan 23 '14 at 09:42
  • Nevertheless, I am using your solution because I don't like Warnings... combined with Gregory's answer, it's problem solved! – Riegardt Steyn Jan 23 '14 at 09:47
  • Added a note on how to install the official schema – Stefan Z Camilleri Jan 23 '14 at 13:58
  • @StefanZCamilleri - the install adds the schema to the current project it should be added to Visual Studio. I got the package using the install and then add it to the correct folder as in the accepted answer – Emond Erno Jun 03 '14 at 10:59
  • Agreed, since NuGet does not have access to those paths. – Stefan Z Camilleri Jun 04 '14 at 10:36
  • 1
    Of note I've recently had issues with adding those namespaces screwing up NuGet. It's unfortunate as I've prefer not to have the warnings. – meh-uk Aug 11 '15 at 16:40
  • I believe these are wrong schemas. These schemas are for .nuspec-s, but not for packages.config. They contain declaration for element in .nuspec-s, but not for the . The accepted solution above works. – DKroot Aug 30 '15 at 17:43
95

You can always make simple xsd schema for 'packages.config' to get rid of this warning. To do this, create file named "packages.xsd":

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
      targetNamespace="urn:packages" xmlns="urn:packages">
  <xs:element name="packages">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="package" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="id" type="xs:string" use="required" />
            <xs:attribute name="version" type="xs:string" use="required" />
            <xs:attribute name="targetFramework" type="xs:string" use="optional" />
            <xs:attribute name="allowedVersions" type="xs:string" use="optional" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Location of this file (two options)

  • In the same folder as 'packages.config' file,
  • If you want to share packages.xsd across multiple projects, move it to the Visual Studio Schemas folder (the path may slightly differ, it's D:\Program Files (x86)\Microsoft Visual Studio 10.0\Xml\Schemas for me).

Then, edit <packages> tag in packages.config file (add xmlns attribute):

<packages xmlns="urn:packages">

Now the warning should disappear (even if packages.config file is open in Visual Studio).

Łukasz Wiatrak
  • 2,718
  • 3
  • 20
  • 36
  • 1
    Don't you have to modify the second line of the XSL: – Uri Nov 14 '11 at 16:52
  • 1
    Do you know why Visual Studio is not creating that xsd file? – Anders Lindén Aug 23 '12 at 06:42
  • 1
    u missed `targetFramework` attribute in xml schema file. I m getting error `targetFramework attribute is not defined` – shashwat Sep 22 '12 at 11:04
  • @harsh, You're right, schema is now updated, thanks Gerardo for fixing it. – Łukasz Wiatrak May 12 '13 at 11:09
  • @ŁukaszWiatrak:Please add the line `` to your example to avoid "The 'allowedVersions' attribute is not declared' (used by modern version of NuGet). Pedantic I know, but the whole post is about fixing a warning that has no ill effect. ;) – Charles Burns Apr 18 '14 at 16:49
  • 2
    I found that using your solution with entity framework, the NuGet console could not parse my package.config correctly and kept telling me that the EnitityFramework package was not installed in my project. I'm using EF 6.1.0 with NuGet 2.8.50313.46 on VS2013 ultimate update 2. – misterfrb Jun 03 '14 at 16:01
  • On Solutions with multiple projects I get Warning "The global element 'urn:packages:packages' has already been declared". I have xsd in the same folder as the packages.config. – n00b Apr 01 '15 at 02:54
  • 7
    This solution breaks the automatic installation of missing packages feature in vs2012/nuget. The warning was gone, but it cost me hours to find out, why vs2012 doesn't install missing packages anymore. So the better solution (answer in http://stackoverflow.com/questions/2833243/web-config-configsource-and-the-xxx-element-is-not-declared-warning) is to create a schema via menu XML -> Create Schema (vs 2012 will automatically add it to the list of used schemas) - no need to change the .config, no break of other features – outofmind Aug 26 '15 at 13:58
  • 1
    I had the same issue as in the previous comment: this solution broke NuGet manager in VS 2013 Update 4. – DKroot Aug 31 '15 at 19:37
  • Presently, I'd add: – as9876 Jul 03 '16 at 08:57
  • 1
    You could create this, and then maintain it over time. We can ask why Microsoft is releasing product with warning errors but then they do this constantly. The impact is that we have to circumvent, or investigate every build to determine whether it's "the same old warnings" or possibly a new one. How about a more professional approach from Microsoft as a solution? – Rick O'Shea Aug 26 '16 at 16:51
  • Visual Studio can automatically create packages.xsd for you: https://stackoverflow.com/questions/6774578/nuget-packages-element-is-not-declared-warning/52714797#52714797 – Hooman Bahreini Oct 09 '18 at 07:24
6

None of the answers will solve your problem permanently. If you go to the path of adding XSD (From Xml menu, select "Create schema"), you will end up having problems with the package manager as it will clean up your packages.config file when you add a new package.

The best solution is just ignore by closing the file when you don't use it.

Baris
  • 639
  • 1
  • 6
  • 7
  • +1; just be aware that [this](http://stackoverflow.com/questions/2833243/web-config-configsource-and-the-xxx-element-is-not-declared-warning) also works (and will not break the package manager). – rsenna May 31 '17 at 01:24
5

The problem is, you need a xsd schema for packages.config.

This is how you can create a schema (I found it here):

Open your Config file -> XML -> Create Schema

enter image description here

This would create a packages.xsd for you, and opens it in Visual Studio:

enter image description here

In my case, packages.xsd was created under this path:

C:\Users\MyUserName\AppData\Local\Temp

Now I don't want to reference the packages.xsd from a Temp folder, but I want it to be added to my solution and added to source control, so other users can get it... so I copied packages.xsd and pasted it into my solution folder. Then I added the file to my solution:

1. Copy packages.xsd in the same folder as your solution

2. From VS, right click on solution -> Add -> Existing Item... and then add packages.xsd

enter image description here

So, now we have created packages.xsd and added it to the Solution. All we need to do is to tell the config file to use this schema.

Open the config file, then from the top menu select:

XML -> Schemas...

Add your packages.xsd, and select Use this schema (see below)

enter image description here

Hooman Bahreini
  • 11,018
  • 7
  • 41
  • 74
3

This happens because VS doesn't know the schema of this file. Note that this file is more of an implementation detail, and not something you normally need to open directly. Instead, you can use the NuGet dialog to manage the packages installed in a project.

David Ebbo
  • 40,353
  • 7
  • 92
  • 112
  • It doesn't matter that you use NuGet to install packages. The same `packages.config' file is built, and it still generates that compiler warning. – ProfK Nov 21 '16 at 05:28
1

This works and remains even after adding a new package:

Add the following !DOCTYPE above the <packages> element:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE packages [
  <!ELEMENT packages (package*)>
  <!ELEMENT package EMPTY>
  <!ATTLIST package
  id CDATA #REQUIRED
  version CDATA #REQUIRED
  targetFramework CDATA #REQUIRED
  developmentDependency CDATA #IMPLIED>
]>
chuckc
  • 141
  • 5
0

Sometimes happens when you have an old project version. To solve it follow these steps:

  1. Right click on References and select packages.config toPackageReference..
  2. Click OK
  3. Wait meanwhile project packages is updating
silexcorp
  • 379
  • 3
  • 6