Hi,


I want to read a VB or C# Visual Studio project file so I can find out which files the project uses (code source files, not images or whatever).

I simply opened a vbproj and csproj file in notepad and found that they are in XML format. A simple example follows. The red parts is what I need:
Code:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
      <blabla... />
   </PropertyGroup>
   <ItemGroup>
      <Reference Include="System.Data" />
      <Reference Include="System.Design" />
   </ItemGroup>
   <ItemGroup>
       <Compile Include="Classes\Errors.cs" />
       <Compile Include="Classes\HttpGet.cs" />
       <Compile Include="Classes\NativeMethods.cs" />
       <Compile Include="Classes\Playlist.cs" />
       <Compile Include="Controls\GradientLabel.cs">
          <SubType>Component</SubType>
       </Compile>
   </ItemGroup>
</Project>
If you want an exact example, just open any of your own project files in notepad and take a look.

I have used the XDocument class and LINQ previously (but in C#) to read an XML file that was very similar and had no problems, but this one just isn't working...

I tried every variation I could think of, but the code that, in my mind, should work is:
vb.net Code:
  1. Dim xml = XDocument.Load(Me.ProjectFilePath)
  2.                 Dim files = From e As XElement In xml.Elements("Project").Single.Elements("ItemGroup") _
  3.                             Where e.Name = "Compile" AndAlso e.HasAttributes AndAlso e.Attribute("Include") IsNot Nothing _
  4.                             Select e.Attribute("Include").Value

However, it seems it cannot even find the Project element, because the query errors saying "Sequence contains no elements" on the 'Single' call...


I can't figure it out. Can someone help me out here? Is there maybe a problem with the file itself? It looks like XML for me but it might not be... Is any file that looks like xml an xml file? I don't think so lol...