【问题标题】:Getting nodes from a XML document by Attribute通过属性从 XML 文档中获取节点
【发布时间】: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 线程的某些代码“似乎对我不起作用”太含糊了。
  • 为什么它对你不起作用?您是否尝试过使用该解决方案?你的代码是什么样的?

标签: c# node.js xml xpath


【解决方案1】:

解决此问题的另一种方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Test {
    class Program {
        public static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("PATHTOXMLFILE");
            List<string> result = RetrieveValues(doc, "Include");
            foreach(string item in result)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }

        public static List<string> RetrieveValues(XmlDocument doc, string attributeName)
        {
            var list = new List<string>();

            string xpath = @"//*[@" + attributeName + "]";
            XmlNodeList xmlNodeList = doc.SelectNodes(xpath);

            foreach (XmlNode xmlNode in xmlNodeList)
            {
                list.Add(xmlNode.Attributes[attributeName].InnerText);
            }

            return list;
        }
    }
}

希望对您有所帮助!

【讨论】:

  • 太棒了!非常感谢
  • 如果解决方案对您有用,请接受答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多