问题
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:
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.
来源:https://stackoverflow.com/questions/6959824/best-way-to-parse-visualstudio-csproj-file-using-python