简体   繁体   English

元素中的“包含”属性<packagereference>无法识别</packagereference>

[英]The attribute "Include" in element <PackageReference> is unrecognized

I created a Directory.Build.props file by right clicking the Solution on Solution Explorer, creating an XML file, and named it so.我通过右键单击解决方案资源管理器上的解决方案创建了一个Directory.Build.props文件,创建了一个 XML 文件,并将其命名。 I then input this XML and tried building but was met with errors:然后我输入这个 XML 并尝试构建但遇到错误:

<Project>
    <PropertyGroup>
        <Version>1.2.3</Version>
        <Authors>John Doe</Authors>
        <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
    </PropertyGroup>
</Project>

The errors say: The attribute "Include" in element <PackageReference> is unrecognized.错误说: The attribute "Include" in element <PackageReference> is unrecognized. and

Project "C:\redacted\Directory.Build.props" was not imported by "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Microsoft.Common.props" at (33,3), due to the file being invalid. ProjectName C:\Users\me\source\redacted\ProjectName\Directory.Build.props   33  

I am so confused here.我在这里很困惑。 The other articles said to locate the MSBuild and I know that its on my machine.其他文章说要找到 MSBuild,我知道它在我的机器上。 The build was working just fine prior to me adding the XML too.在我添加 XML 之前,构建工作正常。 Any guidance would be very appreciated.任何指导将不胜感激。 I am currently on Visual Studio 2022 by the way.顺便说一句,我目前正在使用 Visual Studio 2022。

The PackageReference element needs to be in an ItemGroup - you've got it in a PropertyGroup . PackageReference元素需要在ItemGroup中 - 您已经在PropertyGroup中获得了它。 It should look like this:它应该如下所示:

<Project>
    <PropertyGroup>
        <Version>1.2.3</Version>
        <Authors>John Doe</Authors>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
    </ItemGroup>
</Project>

I had the same kind of problem one time and I found out it was caused by '\xef\xbb\xbf#' which is a 'Unicode BOM(Byte Order Mark)' and consists of invisible characters added by certain text editors like Notepad++, for instance.我曾经遇到过同样的问题,我发现它是由“\xef\xbb\xbf#”引起的,它是一个“Unicode BOM(字节顺序标记)”,由某些文本编辑器(如 Notepad++)添加的不可见字符组成, 例如。 The BOM often functions as a magic number used to pass along information to the program reading the file, such as the Unicode character encoding or endianess but it's presence can interfere with software that does not expect it. BOM 通常用作一个幻数,用于将信息传递给读取文件的程序,例如 Unicode 字符编码或字节序,但它的存在可能会干扰意想不到的软件。 The way I found it by fluke was I had my csproj file on github and when I clicked 'edit', the BOM stuck out like a red dot.我通过侥幸找到它的方式是我在 github 上有我的 csproj 文件,当我点击“编辑”时,BOM 像一个红点一样突出。 I simply backspaced on it and then everything went well.我只是退格了,然后一切顺利。

I came accross the same kind of problem, for me it was due to invalid XML structure:我遇到了同样的问题,对我来说这是由于无效的 XML 结构:

<ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.2.32">
    <PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.3">
    </PackageReference>
</ItemGroup>

As you can see, the first PackageReference tag was not closed, leading to the exact same error The attribute "Include" in element <PackageReference> is unrecognized.如您所见,第一个 PackageReference 标记未关闭,导致完全相同的错误The attribute "Include" in element <PackageReference> is unrecognized. on the second PackageReference tag.在第二个 PackageReference 标记上。

Valid XML structure:有效的 XML 结构:

<ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.2.32"/>
    <PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.3"/>
</ItemGroup>

I hope that can help somebody else !我希望可以帮助别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 元素中的属性“版本”<PackageReference> 不被承认 - The attribute "Version" in element <PackageReference> is unrecognized 加载csproj元素中的属性“ExcludeAssets”<packagereference> 无法识别</packagereference> - Load csproj The attribute “ExcludeAssets” in element <PackageReference> is unrecognized 加载项目时出错:无法识别属性包含 - Error while loading project: Attribute Include is unrecognized 如何使用 Package.props 文件转换 .csproj 中 PackageReference 属性的 Include 属性值? - How to transform Include attribute value of PackageReference property in .csproj using Package.props file? 删除元素的定制配置无法识别的属性 - Custom configuration unrecognized attribute for remove element PackageReference 条目中的 Version 属性是否有任何有意义的区别? - Is there any meaningful difference where the Version attribute is in a PackageReference entry? 无法识别的属性“defaultAlgorithmSuite” - Unrecognized attribute 'defaultAlgorithmSuite' 无法识别的元素 unitTestProvider - Unrecognized element unitTestProvider 连接字符串中无法识别的元素 - Unrecognized element in connection string 无法识别的元素:“ binaryMessageEncoding” - Unrecognized element : 'binaryMessageEncoding'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM