I'm trying to read an XML File for concatenating values of specific attributes. I have an element with bunch of Attributes for it, as shown below:
<storeSurvey Annualsales="150000" BankName="Primary Bank" BusinessType="05" YearOpened="1980" Location="New Hampshire"/>
<storeSurvey Annualsales="300000" BankName="Flagstar Bank" BusinessType="07" YearOpened="1993" Location="Michigan"/>
<storeSurvey Annualsales="250000" BankName="Stifel" BusinessType="02" YearOpened="1890" Location="Missouri"/>
<storeSurvey Annualsales="500000" BankName="Frost Bank" BusinessType="08" YearOpened="1868" Location="Texas"/>
<storeSurvey Annualsales="750000" BankName="Webster Bank" BusinessType="05" YearOpened="1935" Location="Connecticut"/>
<storeSurvey Annualsales="950000" BankName="CIT Group" BusinessType="02" YearOpened="1908" Location="New York"/>
I'm trying to retrieve information from the above xml file based on the BankName for the attribute values like YearOpened and Location.
I already tried the following logic but getting some errors. I'm fairly new to xml querying and any suggestions will be appreciated.
public static void Main(string[] args)
{
string[] arguments = Environment.GetCommandLineArgs();
args[0] = Path.GetDirectoryName(args[0]);
DataLocation = Path.Combine(args[0], "ListofBanks.xml");
// ReaderOptions Data from XML file and retriving Data
XDocument xml = XDocument.Load(DataLocation);
var criteria = new[] { "Stifel", "Frost Bank", "Primary Bank"};
var items = from item in xml.Root.Descendants("storeSurvey")
where item.Attribute("BankName").Value.Contains(criteria)
select new
{
Founded = (string)item.Attribute("YearOpened"),
HeadQuarters = (string)item.Attribute("Location"),
};
foreach(var value in items)
{
Console.WriteLine(value.Founded + " " + value.HeadQuarters);
}
Console.Read();
}
With the above logic, I'm getting errors in the where condition for criteria (Argument1: Cannot convert from String[] to string and also in the foreach condition "items" - foreach statement cannot operate on variables of type '?' because '?' doesnot contain a public instance definition of 'GetEnumerator'.
I'm trying to achieve the following on my console window:
1890 Missouri
1868 Texas
1980 New Hampshire