【发布时间】:2016-06-24 12:40:28
【问题描述】:
我的代码可以读取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% 的情况下都能完美运行。今天,当它试图从下面的代码中获取 assembly 名称时,它得到了 Null Object Reference Exception。顶部的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>
【问题讨论】: