Sudha Vuppala
Sudha Vuppala

Reputation: 9

Linq to XML - to read a values of multiple attributes

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

Upvotes: 0

Views: 966

Answers (2)

Gustavo Oliveira
Gustavo Oliveira

Reputation: 127

You can use the code shared by jdweng and apply a Linq Expression WhereClause

class Program
{
    const string FILENAME = @"c:\temp\test.xml";

    static void Main(string[] args)
    {
        string[] validBankNames = new [] { "Stifel", "Frost Bank", "Primary Bank" };

        XDocument xDocument = XDocument.Load(FILENAME);

        var results = xDocument.Descendants("storeSurvey")
                        .Select(x => new Bank
                        {
                            AnnualSales = (int)x.Attribute("Annualsales"),
                            BankName = (string)x.Attribute("BankName"),
                            BusinessType = (string)x.Attribute("BusinessType"),
                            YearOpened = (int)x.Attribute("YearOpened"),
                            Location = (string)x.Attribute("Location")
                        })
                        .Where(bank=> validBankNames.Contains(bank.BankName))
                        .ToList();

        results.ForEach(x => Console.WriteLine($"{x.YearOpened} {x.BankName}"));

        Console.ReadKey();
    }
}

public class Bank
{
    public string BankName { get; set; }
    public int AnnualSales { get; set; }
    public string BusinessType { get; set; }
    public int YearOpened { get; set; }
    public string Location { get; set; }
}

Upvotes: 0

jdweng
jdweng

Reputation: 34421

Use following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            var results = doc.Descendants("storeSurvey").Select(x => new
            {
                sales = (decimal)x.Attribute("Annualsales"),
                bank = (string)x.Attribute("BankName"),
                businessType = (int)x.Attribute("BusinessType"),
                opened = (int)x.Attribute("YearOpened"),
                location = (string)x.Attribute("Location")
            }).ToList();
        }

    }
}

Upvotes: 1

Related Questions