我有读取proj文件并检查其assembly名称的代码。
XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition = XDocument.Load(projPath);
assemblyName = projDefinition
.Element(msbuild + "Project")
.Element(msbuild + "PropertyGroup")
.Element(msbuild + "AssemblyName")
.Value;
上面的代码在 99% 的时间里都能完美运行。今天它Null Object Reference Exception试图assembly从下面的代码中获取名称。顶部property group element和import element通常朝向proj文件的底部。
我的问题是为什么XDocument不过去Import Element也不接其他propertygroup elements?
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<UseGlobalApplicationHostFile />
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Some Elements ...
<AssemblyName>AssemblyNameGoesHere</AssemblyName>