【发布时间】:2017-08-17 08:47:11
【问题描述】:
我想从 XLM 文件中获取带有 Xpath 的节点列表。我想通过仅搜索某个属性来获得这些。这是 Xml 文件。
<ItemGroup>
<Compile Include="Algorithmus_Müllabfuhr\Algorithm.cs" />
<Compile Include="Algorithmus_Müllabfuhr\Tests\AlgorithmTest.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
我的输出应该是
Algorithmus_Müllabfuhr\Algorithm.cs
Algorithmus_Müllabfuhr\Tests\AlgorithmTest.cs
Form1.cs
Form1.Designer.cs
程序.cs
属性\AssemblyInfo.cs
Form1.resx
属性\资源.resx
Properties\Resources.Designer.cs
packages.config
属性\Settings.settings
属性\Settings.Designer.c
到目前为止,我只搜索具有“编译”元素的节点。并获得价值。现在我需要从具有“包含”属性的节点中获取所有值。
var doc = new XmlDocument();
doc.Load(csproj.FullName);
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/msbuild/2003");
XmlNodeList xnList = doc.SelectNodes(@"/msbld:Project/msbld:ItemGroup/msbld:Compile", ns);
foreach (XmlNode node in xnList)
{
referencedfiles.Add(new FileInfo(Path.Combine(csproj.Directory.FullName, node.Attributes["Include"].Value)));
}
CompareLists(filesinfiles, referencedfiles);
Stackoverflow select all xml nodes which contain a certain attribute 我看着这个线程,它似乎对我不起作用。 XML 文档中的命名空间也有问题。
【问题讨论】:
-
您似乎正在编写规范并期待解决方案。说来自另一个 SO 线程的某些代码“似乎对我不起作用”太含糊了。
-
为什么它对你不起作用?您是否尝试过使用该解决方案?你的代码是什么样的?