[XML] Reading Visual Studio Project file
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:
Dim xml = XDocument.Load(Me.ProjectFilePath)
Dim files = From e As XElement In xml.Elements("Project").Single.Elements("ItemGroup") _
Where e.Name = "Compile" AndAlso e.HasAttributes AndAlso e.Attribute("Include") IsNot Nothing _
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...
Re: [XML] Reading Visual Studio Project file
Hope this helps.
I tried the following in VS2008 reading a VS2010 project file
Code:
Imports <xmlns:alias="http://schemas.microsoft.com/developer/msbuild/2003">
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim doc = XDocument.Load("DynamicClassXML.vbproj")
Dim Query = (From P In doc...<alias:Project> _
Where P.@ToolsVersion = "4.0" _
Select P).FirstOrDefault
Console.WriteLine("[{0}]", Query.ToString)
End Sub
End Class
which returned everything. Then I tried for a smaller chunk
Code:
Imports <xmlns:alias="http://schemas.microsoft.com/developer/msbuild/2003">
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim doc = XDocument.Load("DynamicClassXML.vbproj")
Dim Query = (From P In doc...<alias:Project> _
Where P.@ToolsVersion = "4.0" From X In P.<alias:PropertyGroup> _
Where X.<alias:AssemblyName>.Value = "VB 10 Generic Iterator Sample" _
Select X).FirstOrDefault
Console.WriteLine("[{0}]", Query.ToString)
End Sub
End Class
Re: [XML] Reading Visual Studio Project file
Thanks, I got it to work that way... I still don't really understand what's going on. What does the Import do and why do I need the 'alias' thingy..?
Anyway, this is the code that seems to retrieve the files correctly:
vb.net Code:
Dim xml = XDocument.Load(Me.ProjectFilePath)
Dim files = From p In xml...<alias:Project> _
From i In p...<alias:ItemGroup> _
From c In i...<alias:Compile> _
Where c.HasAttributes AndAlso c.Attribute("Include") IsNot Nothing _
Select c.@Include
Thanks!
Re: [XML] Reading Visual Studio Project file
Quote:
Originally Posted by
NickThissen
Thanks, I got it to work that way... I still don't really understand what's going on. What does the Import do and why do I need the 'alias' thingy..?
Anyway, this is the code that seems to retrieve the files correctly:
vb.net Code:
Dim xml = XDocument.Load(Me.ProjectFilePath)
Dim files = From p In xml...<alias:Project> _
From i In p...<alias:ItemGroup> _
From c In i...<alias:Compile> _
Where c.HasAttributes AndAlso c.Attribute("Include") IsNot Nothing _
Select c.@Include
Thanks!
Using the import provides IntelliSense when working the LINQ statement. I found this out when trying out some code from Bill McCarthy on his Snippet editor several years ago.
Re: [XML] Reading Visual Studio Project file
LINQ to XML doesn't work (except how kevin showed you) when the root element contains an xml namespace