Best way to parse VisualStudio .csproj file using python

。_饼干妹妹 提交于 2019-12-07 08:18:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!