Shell
Shell

Reputation: 1

Getting nodes from a XML document by Attribute

I want to get a List of nodes with Xpath from XLM file. I want to get those by searching only for a certain Attribute. This is the Xml file.

<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>

My Output should be

Algorithmus_Müllabfuhr\Algorithm.cs

Algorithmus_Müllabfuhr\Tests\AlgorithmTest.cs

Form1.cs

Form1.Designer.cs

Program.cs

Properties\AssemblyInfo.cs

Form1.resx

Properties\Resources.resx

Properties\Resources.Designer.cs

packages.config

Properties\Settings.settings

Properties\Settings.Designer.c

Up to this point i was only searching for nodes with the element "Compile". And getting the value. Now I need to get all Values from nodes with the Attribute "Include".

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 I looked at this thread and it doensn´t seem to work for me. I also got a problem with the namespace in the XML document.

Upvotes: 0

Views: 698

Answers (1)

Rehan Shikkalgar
Rehan Shikkalgar

Reputation: 1047

Another Way to solve this Problem:

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;
        }
    }
}

Hope this Helps!

Upvotes: 1

Related Questions