I want to create a tool that manages C # project files

(Basically we want to import target files in some places in the * .csproj file and otherwise check if the file meets our standards)

What is the best way to do this? I plan to use C # and XDocument (LINQ to XML) and manipulate nodes. But is this the best way? Other alternatives may be:

  • Find the XSD for csproj / msbuild and create strict type objects that represent the project file. is it possible?
  • Are there any other tools / languages ​​that are better (he can use this as an excuse for learning Ruby). The answer would have to have some links to examples for manipulating XML.

Perhaps the real question is: what's the best way to read and manipulate XML programmatically?

+7
c # xml msbuild
source share
4 answers

I would personally use LINQ to XML directly ... if you are not doing really heavy processing, I do not think it would be advisable to create an object model for it. How many rules / what do you need for this tool?

+5
source share

MSBuild (which is the project file format) has a model object that you can use for such applications.

+5
source share

We have our own tool that manages project files on the fly to update links and set various properties. Since project files are not so complex, we simply manipulate XML directly. The tool was written long before LINQ was available, so it does not use it, but I see no reason why you could not do what Jon offers.

When we upgraded from VS2005 to VS2008, we had to make minor code adjustments, but, of course, there are no guarantees. We may need to adjust future versions more.

If you're interested, a little more information about our tool in my answer to this question. Should .sln be tied to the source control?

+3
source share

According to the second part of our question, how to manipulate XML programmatically, here are a few xml from the project configuration file. (Web.config)

<system.web> <compilation debug="false"> <assemblies> <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, </compilation> </compilation> 

and parse using LINQ-TO-XML , something like

 XDocument assemblies = XDocument.Parse("web.config"); var projectAssemblies= from a in assemblies.Descendants("system.web\assemblies") select new // What ever object you waant { Assembly = a.Attribute("assembly").Value.ToString(), Version = a.Attribute("Version").Value.ToString(), Culture = a.Element("Culture").Value.ToString(), PublicKeyToken = a.Attribute("PublicKeyToken").Value.ToString() }; foreach (var v in projectAssemblies) { // enjoy them } 

hope this helps

+2
source share

All Articles