0

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

2 Answers 2

1

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

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for responding @jdweng. But your logic doesn't filtering any thing i.e., I'm trying to filtered the output for only specific criteria: banknames. Also, I'm trying to write the output to console window, your logic doesn't contain any console.WriteLine statement. So, I don't think looking for this output. your logic is simply reading the xml file for all parameters and storing into the results variable.
I did the hard work. It is easy to filter the results from my query.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.