5

What is the best way to parse Visual Studio .csproj file using python, for further modification?

It is some .csproj file:

...
<ItemGroup>
    <Reference Include="SomeAssembly, Version=1.1.1.1"/>      
...
</ItemGroup>
...

I want to insert this:

<HintPath>path/to/SomeAassembly.dll</HintPath>

into the <Reference/> node.

1
  • Better than finding string like "Reference" and split other part of string with regular expression. And, after that, insert HintPath tag and close Reference tag. Commented Aug 5, 2011 at 19:53

1 Answer 1

2

VS project files are XML. Python comes with a number of XML parsing libraries: http://docs.python.org/library/markup.html

Either of xml.parsers.expat or xml.minidom could accomplish what you describe. For processing small XML documents like VS project files, choosing among them is a matter of taste. The example code in the documentation for each should help you decide.

An elegant alternative, especially if you have many such transformations to apply, would be to use a filter object with the xml.sax API.

Sign up to request clarification or add additional context in comments.

7 Comments

I used minidom. Unfortunately, Visual Studio has a very specific formatter used for .csproj files, and my Python script modified the formatting in many aspects, which created false diffs in the SCM. Any ideas how one can address this?
If minimising diffs is very important to you, then maybe an XML parser isn't the way to go, and it's better to treat this as a string transformation problem, i.e. attack it with regexp substitutions. That'll be much more brittle though.
That's what I thought I might employ next time I have a similar task. Thanks for sharing! Maybe this concern will be relevant for Eugen as well.
Would it help to make two commits to the SCM: One after simply loading and toprettyxmling the file in order to normalise the formatting, and then another with the actual change?
...And then opening every modified solution in Visual Studio and forcing Visual Studio to save it (with its normal formatting). Otherwise, next time you do a normal change (e.g. add a file) you'll get false diffs again, albeit in the other direction. Maybe regexps are not so bad, after all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.