C# 按属性从XML文档获取节点

C# 按属性从XML文档获取节点,c#,node.js,xml,xpath,C#,Node.js,Xml,Xpath,我想从XLM文件中获得一个包含Xpath的节点列表。我只想通过搜索某个属性来获取这些属性。这是Xml文件 <ItemGroup> <Compile Include="Algorithmus_Müllabfuhr\Algorithm.cs" /> <Compile Include="Algorithmus_Müllabfuhr\Tests\AlgorithmTest.cs" /> <Compile Include="Form1.cs"> <

我想从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>

我看着这条线,它似乎对我不起作用。XML文档中的名称空间也有问题。

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

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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Xml;
名称空间测试{
班级计划{
公共静态void Main(字符串[]args)
{
XmlDocument doc=新的XmlDocument();
文件加载(“路径文件”);
列表结果=检索值(文档,“包括”);
foreach(结果中的字符串项)
{
控制台写入线(项目);
}
Console.Read();
}
公共静态列表检索值(XmlDocument文档,字符串属性名)
{
var list=新列表();
字符串xpath=@”/*[@“+attributeName+”];
XmlNodeList XmlNodeList=doc.SelectNodes(xpath);
foreach(xmlNodeList中的XmlNode)
{
添加(xmlNode.Attributes[attributeName].InnerText);
}
退货清单;
}
}
}

希望这有帮助

您似乎正在编写一个规范,并期待一个解决方案。声明来自另一个SO线程的一些代码“似乎对我不起作用”太模糊了。为什么它对你不起作用?你试过使用这个解决方案吗?你的代码是什么样子的?真糟糕!非常感谢,如果解决方案对您有效,请接受答案!
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;
        }
    }
}