PigsIncorporated
PigsIncorporated

Reputation: 153

Iterate through config file to get settings values

This is a config file I have:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectivity.ExtensionSettings3>
    <extension
     interface="Autodesk.Connectivity.JobProcessor.Extensibility.IJobHandler, Autodesk.Connectivity.JobProcessor.Extensibility"
     type="Job.JobExtension, Job">
      <setting key="JobType1" value="Name1"/>
      <setting key="JobType2" value="Name2"/>
    </extension>
  </connectivity.ExtensionSettings3>
</configuration>

I need to iterate through the config file in a C# application to get all the setting key values whose name contains "JobType".

This is the code I was using but it returns no results:

    JobNames = xml.Descendants().ToList().Where(xe => xe.Name.LocalName.Contains("JobType")).ToList();

    foreach (System.Xml.Linq.XElement strJobName in JobNames)
    {
        Console.WriteLine(strJobName.Value);
    }

What am I doing wrong?

Upvotes: 2

Views: 156

Answers (1)

EylM
EylM

Reputation: 6103

First you need to select all "setting" nodes. After it, you need to query the "attribute" "key".

Here is the code:

var JobNames = xml.Descendants("setting").Where(xe => xe.Attribute("key").Value.Contains("JobType")).ToList();

foreach (System.Xml.Linq.XElement strJobName in JobNames)
{
    Console.WriteLine(strJobName.LastAttribute.Value);
}

Upvotes: 2

Related Questions