Results 1 to 5 of 5

Thread: [XML] Reading Visual Studio Project file

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    [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:
    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...

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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
    Last edited by kareninstructor; Jul 9th, 2011 at 10:15 PM.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. Dim xml = XDocument.Load(Me.ProjectFilePath)
    2.                 Dim files = From p In xml...<alias:Project> _
    3.                             From i In p...<alias:ItemGroup> _
    4.                             From c In i...<alias:Compile> _
    5.                             Where c.HasAttributes AndAlso c.Attribute("Include") IsNot Nothing _
    6.                             Select c.@Include
    Thanks!

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: [XML] Reading Visual Studio Project file

    Quote Originally Posted by NickThissen View Post
    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:
    1. Dim xml = XDocument.Load(Me.ProjectFilePath)
    2.                 Dim files = From p In xml...<alias:Project> _
    3.                             From i In p...<alias:ItemGroup> _
    4.                             From c In i...<alias:Compile> _
    5.                             Where c.HasAttributes AndAlso c.Attribute("Include") IsNot Nothing _
    6.                             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.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width