A .csproj file is an MSBuild project file: XML following the MSBuild
schema, which Microsoft publishes as Microsoft.Build.xsd in the
dotnet/msbuild repository. The same
schema applies to .vbproj, .fsproj, .vcxproj,
.props and .targets files. The items and properties that give
a project its meaning are on the items and
properties page.
Elements
| Element | Children | Attributes |
|---|---|---|
Project | Choose, Import, ImportGroup, ItemGroup, ItemDefinitionGroup, ProjectExtensions, PropertyGroup, Sdk, Target, UsingTask | DefaultTargets, InitialTargets, Sdk, ToolsVersion, TreatAsLocalProperty, xmlns |
PropertyGroup | properties | Condition, Label |
| Property (any name) | – | Condition, Label; the text is the value |
ItemGroup | items | Condition, Label |
| Item (any name, the item type) | metadata | Include, Exclude, Remove, Update, MatchOnMetadata, MatchOnMetadataOptions, KeepDuplicates, KeepMetadata, RemoveMetadata, Condition, Label; other attributes are metadata |
| ItemMetadata (any name) | – | Condition; the text is the value |
ItemDefinitionGroup | items | Condition, Label; sets default metadata for every item of a type |
Import | – | Project, Condition, Label, Sdk, Version, MinimumVersion |
ImportGroup | Import | Condition, Label |
Sdk | – | Name (required), Version, MinimumVersion |
Choose | When, Otherwise | Label |
When | Choose, ItemGroup, PropertyGroup | Condition (required) |
Otherwise | Choose, ItemGroup, PropertyGroup | Label |
Target | tasks, PropertyGroup, ItemGroup, OnError | Name, Condition, Inputs, Outputs, Returns, KeepDuplicateOutputs, BeforeTargets, AfterTargets, DependsOnTargets, Label |
| Task (named after the task) | Output | Condition, ContinueOnError, Architecture, Runtime, the task’s parameters |
Output | – | TaskParameter, PropertyName or ItemName, Condition |
OnError | – | ExecuteTargets (required), Condition, Label; must be last in its target |
UsingTask | ParameterGroup, Task | TaskName, AssemblyName or AssemblyFile, TaskFactory, Architecture, Runtime, Override, Condition |
ParameterGroup | parameters | – |
| Parameter (any name) | – | ParameterType, Required, Output |
Task (inside UsingTask) | data for the task factory | Evaluate |
ProjectExtensions | any XML | –; at most one, for data that is not MSBuild’s |
Project
| Attribute | Meaning |
|---|---|
Sdk | An SDK name with an optional /version, e.g. Microsoft.NET.Sdk. MSBuild adds an implicit <Import Project="Sdk.props" Sdk="…"/> at the top of the project and <Import Project="Sdk.targets" Sdk="…"/> at the bottom. |
DefaultTargets | Targets to build when none is named, separated by semicolons. Without it, the first target after imports are evaluated is used. |
InitialTargets | Targets that run before the default or named targets; from every imported file, in import order. |
ToolsVersion | The toolset version, which sets $(MSBuildBinPath) and $(MSBuildToolsPath). |
TreatAsLocalProperty | Properties that global (command-line) properties do not override in this file and its later imports. |
xmlns | If present, http://schemas.microsoft.com/developer/msbuild/2003; not needed since Visual Studio 2017. |
Items
| Attribute | Meaning |
|---|---|
Include | Files or wildcards to add (*, ?, and ** for any depth), separated by semicolons. |
Exclude | Files or wildcards not to add. |
Remove | Files or wildcards to remove from the item list. |
Update | Change the metadata of items already in the list; outside targets only. |
MatchOnMetadata | With a Remove that only references other items, match on these metadata names instead of the item values. |
MatchOnMetadataOptions | CaseSensitive, CaseInsensitive (default) or PathLike. |
KeepDuplicates | Whether to add an exact duplicate; inside targets only. |
KeepMetadata | Only these metadata names are copied from source items; inside targets only. |
RemoveMetadata | These metadata names are not copied; inside targets only. |
Condition | See conditions. |
Any other attribute, or any child element, is metadata: e.g.
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />.
Import
Project (required) is the path of the file to import, relative to the
importing file, and may contain wildcards: the matches are imported in sorted order.
Sdk names an SDK to find the file in. Imported files have the same schema;
by convention they end in .props or .targets.
MSBuildProjectDirectory and similar reserved properties refer to the
importing project.
Target
| Attribute | Meaning |
|---|---|
Name | Required. Any characters except $@()%*?.. The last target with a given name wins. |
Condition | If false, neither the target nor its DependsOnTargets run. |
Inputs, Outputs | Files whose timestamps are compared to decide whether the target is up to date. |
Returns | Items returned to callers; without it, Outputs are returned. |
KeepDuplicateOutputs | true: keep duplicate items in the returned list (default false). |
BeforeTargets, AfterTargets | Run this target before or after the named targets. |
DependsOnTargets | Targets to run first, left to right. |
Label | An identifier. |
A target runs at most once per build.
Tasks
A task element is named after the task (e.g. Csc, the C# compiler
task); its attributes set the task’s parameters, and Output
children store its outputs in properties or items.
ContinueOnError values:
| Value | Meaning when the task fails |
|---|---|
WarnAndContinue or true | Carry on; its errors become warnings. |
ErrorAndContinue | Carry on; its errors stay errors. |
ErrorAndStop or false | The default: stop the target and the build, which fails; OnError targets still run. |
UsingTask
| Attribute | Meaning |
|---|---|
TaskName | Required. The task, with its namespace where needed. |
AssemblyName / AssemblyFile | One is required: the assembly by name, or by path (relative to the declaring file). |
TaskFactory | The class that creates the task; default AssemblyTaskFactory. |
Architecture | x86, x64, CurrentArchitecture or *. |
Runtime | CLR2, CLR4, CurrentRuntime, NET (MSBuild 18 and later) or *. |
Override | true: take priority over other definitions of the task (MSBuild 17.2 and later). Otherwise the first UsingTask for a name wins. |
Conditions
Condition is allowed on Import, ImportGroup,
items, ItemDefinitionGroup, ItemGroup, metadata,
OnError, Output, properties, PropertyGroup,
Target, tasks, UsingTask and When.
| Expression | Meaning |
|---|---|
'a' == 'b', 'a' != 'b' | String comparison, ignoring case. Quotes are needed for empty values. |
<, >, <=, >= | Numeric comparison of decimal, 0x hexadecimal or four-part version values (written < and > in XML). |
Exists('path') | A file or folder exists (no wildcards). |
HasTrailingSlash('s') | The string ends in \ or /. |
!, And, Or, ( ) | Logic; And binds tighter than Or. |
Expansions
| Form | Meaning |
|---|---|
$(Name) | A property’s value; empty if not defined. String methods may follow, e.g. $(TargetFramework.TrimEnd(`0123456789`)). |
@(Type), @(Type, 'sep') | An item list joined by semicolons (or by sep). |
@(Type->'%(Meta)') | An item transform. |
%(Meta), %(Type.Meta) | Item metadata; inside targets, causes batching. |
What MSBuild refuses
The structure rules below are enforced when the file is loaded; each was checked by
running dotnet msbuild on a file that breaks it. The explorer reports each
one, with the code, on the element or attribute concerned. The schema (the
.xsd) is looser than MSBuild: it also requires a target or import, which
SDK-style projects do not have.
| Code | Rule |
|---|---|
MSB4068 | The root element is Project. |
MSB4041 | The default namespace is the MSBuild 2003 namespace or none. |
MSB4067 | An element is allowed where it stands: a Project holds only the elements of the table above; text between elements of a group is refused (#text); a task holds only Output. |
MSB4066 | An attribute is allowed on its element: PropertyGroup, a property, Target, Import and item metadata accept only those in the table; an item accepts any (they are metadata); a task accepts any (they are parameters); Project accepts any. Also Exclude with Remove, and Include in an ItemDefinitionGroup. |
MSB4035 | A required attribute is present and not empty: Target Name, Import Project, When Condition, OnError ExecuteTargets, UsingTask TaskName. |
MSB4111 | An item has at most one of Include, Remove and Update. |
MSB4232 | An item outside a target has Include, Update or Remove. |
MSB4004 | A property is not one MSBuild reserves (the MSBuildProject… and MSBuildThisFile… names and the other reserved properties) and is not named like an MSBuild element (Choose, Target…). |
MSB5016 | A property, item or metadata name has no character other than letters, digits, _ and - (a . is refused although XML allows it). |
MSB4072 | A UsingTask has AssemblyName or AssemblyFile, not both, unless it names a TaskFactory. |
MSB4029 | An Output has TaskParameter and exactly one of PropertyName and ItemName. |
MSB4084, MSB4083 | A Choose has a When, no When after the Otherwise, and one Otherwise at most. |
MSB4038 | OnError elements come last in a target. |
MSB4079 | There is one ProjectExtensions. |
MSB4238 | An Sdk name is not empty and has a version after a slash. |
MSB4021 | ContinueOnError is true, false, WarnAndContinue, ErrorAndContinue or ErrorAndStop. |
MSB4025 | The file is well-formed XML (an element name cannot start with a digit). |
| conditions | See conditions. |
Sources
Microsoft Learn (MSBuild): MSBuild project file schema reference, and the element pages for Project, Item, Import, Target, Task and UsingTask; MSBuild conditions; .NET project SDK overview (implicit imports).