简体   繁体   中英

How can I determine if an xml file is a valid msbuild build file?

How can I determine if a file is a valid msbuild file from a directory, which may have many xml files? I am reading all files in the directory, parsing them using BeautifulSoup, determining if they have a project element, and either of itemgroup , target or compile elements, but this approach is returning many false positives. Is it possible to make this stricter?

For starters recognize that you should be using a standard naming convention for msbuild files. The industry uses, *.vcxproj, *.csproj, *.vbproj, (etc..), *.targets, *.props, *.proj (sometimes), and even less *.xml.

Second you should use the the msbuild project API to do all the heavy guess work for you. Don't reinvent the wheel: https://docs.microsoft.com/en-us/dotnet/api/microsoft.build.evaluation.project?view=netframework-4.8

Using that, you could use the Project class to load an msbuild file like so:

var p = new Microsoft.Build.Evaluation.Project("C:\somefile.targets");

And then catch an exception if it's NOT an msbuild file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM