簡體   English   中英

通過屬性從XML文檔獲取節點

[英]Getting nodes from a XML document by Attribute

我想從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\\測試\\ AlgorithmTest.cs

Form1.cs的

Form1.Designer.cs

Program.cs中

屬性\\ AssemblyInfo.cs中

Form1.resx中

屬性\\ Resources.resx

屬性\\ 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選擇了所有包含特定屬性的xml節點,我看着這個線程,它似乎對我不起作用。 我也遇到了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;
        }
    }
}

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM