Best way to parse VisualStudio .csproj file using

2019-05-04 20:07发布

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条回答
等我变得足够好
2楼-- · 2019-05-04 20:53

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.

查看更多
登录 后发表回答