83

I'm looking at tidying up my project layout in Visual Studio and I'm wondering if there is any hack, plugin or trick to associate an .xml file with a .cs file of the same name so they appear grouped in my solution navigator/explorer.

Similar to the way the code-behind file is associated with its aspx.

alt text

Any suggestions welcome. Thanks

g t
  • 7,287
  • 7
  • 50
  • 85
mat-mcloughlin
  • 6,492
  • 12
  • 45
  • 62
  • **Related:** See [this answer](https://stackoverflow.com/a/55788856/1497596) for how to nest (group) `.cs` files in a .NET Core project. – DavidRR Mar 06 '20 at 17:56
  • CMakes's [source_group](https://cmake.org/cmake/help/latest/command/source_group.html) may be a help. – Laurie Stearn Jan 04 '23 at 12:53

6 Answers6

79

In your project file :

<Compile Include="FileA.cs"/>
<Compile Include="FileA.xml">
  <DependentUpon>FileA.cs</DependentUpon>
</Compile>

Or you could use Group Items command of VSCommands 2010 extension.

Edit: Just in case your file is in a folder, don't include the folder name in DependentUpon tag. For example if your file is in Helpers folder:

<Compile Include="Helpers\FileA.cs"/>
<Compile Include="Helpers\FileA.xml">
  <DependentUpon>FileA.cs</DependentUpon>
</Compile>
Babak
  • 1,274
  • 15
  • 18
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117
  • 2
    Perfect! I didn't really think it would be possible – mat-mcloughlin Sep 01 '10 at 11:46
  • I have found that in a VS2010 VB project, if you are trying to add a child class (eg, partial) to a XAML control, the child file will be hidden unless you choose "Show All Files". Confusing, so just add .xaml.vb onto the end of it and then it's displayed. – Rocklan Oct 18 '13 at 01:17
  • 1
    I was confused on how to use the VSCommands Group function until I saw [this](http://www.rapidstreams.net/2012/08/grouping-partial-classes-in-visual.html) post. To use VSCommands group function, HIGHLIGHT ALL the files you want to group, then RIGHT click on ANY one of them, then click group. – Chris Aug 06 '14 at 23:47
  • 1
    @Julien, while I was going to vote this down, I thought better of it because technically this does nest files and that's what the question was asking. What most aren't aware of and the inspiration for my initial reaction to downvote was because doing this breaks VS's ability to rename files. See my answer here for more information and why we ultimately decided against doing this. – Mark A. Donohoe Aug 19 '15 at 21:03
  • For VS2017, I had to use – Jamie Burns Jun 20 '17 at 12:03
58

If you do not want to slow down you IDE with heavy and proprietary VSCommands extension you can use small extension NestIn instead. It can nothing but group/ungroup files

Dao
  • 2,784
  • 3
  • 28
  • 36
  • Very handy, but it can't rename nested items. – stian.net Sep 14 '12 at 09:41
  • 6
    Also this one: File Nesting https://visualstudiogallery.msdn.microsoft.com/3ebde8fb-26d8-4374-a0eb-1e4e2665070c] – Tohid Aug 20 '15 at 18:43
  • 1
    @Tohid, your link to the [File Nesting Extension](https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting) is too valuable for being a simple comment. It would be better to be posted as an answer, I guess. – AlexMelw Mar 20 '18 at 19:24
3

In .NET (Core+)

<ItemGroup>
  <Compile Update="FileA.*.cs">
    <DependentUpon>FileA.cs</DependentUpon>
  </Compile>
</ItemGroup>

Note that Update is used instead of Include where files are already (implicitly) included in the project and use of Include causes compile-time "Error NETSDKxxxx Duplicate 'Compile' items were included... The duplicate items were: 'FileA.xxxx.cs' C:\Program Files\dotnet\sdk\x.x.x.x\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.Shared.targets"

See Is there a DependentUpon option in a .net core app?

AlanK
  • 1,827
  • 13
  • 16
2

For the simple case where the file is a "top level" file, Julien's description works perfectly. However, in the case where the DependentUpon file is in a Folder under the project, this looks different. I personally don't like it because it seems like it could lead to ambiguity, but that's an opinion.

<Compile Include="DataStructs\CKDTree.cs" />
<Compile Include="DataStructs\CClosestObjects.cs" >
    <DependentUpon>CKDTree.cs</DependentUpon>
</Compile>

Notice that the dependent item does NOT include the Folder of the parent. This is true in VS2013... probably true in earlier versions but I have not verified it.

Veldaeven
  • 319
  • 1
  • 13
  • Thanks, the "missing" folder bit is good to know. Was wondering why it didn't turn up correctly in VS2013. – John Korsnes Dec 09 '14 at 10:13
  • I believe it's not needed because it makes the assumption that the related 'parent' file (sans path) is a sibling of the 'child' file and the child does specify a path. A simple test of adding a second CKDTree.cs file in a different location, then creating a child in that other location but with the exact same `DependentUpon` element would confirm this hypothesis. Still, as I called out in my answer below, this breaks VS's ability to rename files so be wary of doing this. – Mark A. Donohoe Aug 19 '15 at 21:05
2

File Nesting extension for visual studio is a good one. It has about 500K downloads at the time of writing this answer. I added it personally to my VS 2015 and it was working fine (haven't tried it with VS 2017 yet).

Tohid
  • 6,175
  • 7
  • 51
  • 80
0

Not sure if people are aware, but nesting files like this seemingly breaks VS's ability to rename the root file, at least when your new nested file is also a partial class. For instance here's the tree we created...

MainWindow.xaml
    MainWindow.xaml.cs
    MainWindow.Commands.cs

MainWindow.Commands.cs is just another partial class of MainWindow, same as MainWindow.xaml.cs. However, if you then try and rename MainWindow.xaml, instead of automatically renaming the dependent files, it throws an exception.

For completeness, I also tried naming the file MainWindow.xaml.Commands.cs but that didn't work either.

Without the extra 'commands' file, rename works fine, of course.

MainWindow.xaml
    MainWindow.xaml.cs

Anyway, this was reason enough for us to abandon nesting files like this. Without the ability to rename, it's just not worth it.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286