0

this is my SearchFile.xml that im using to filter the chippers by county

<search>

<chipper LName="Nico's Takeaway"
    PlAddress= "Unit 12 River Oaks, Claregalway"
    County="Galway"
    PhoneNumber =" 091 799791"/>

  <chipper LName=" Martino's Takeaway"
    PlAddress= "The Square, Dunmore"
    County ="Galway"
    PhoneNumber="093 39720"/>
  <chipper
    LName="Attico's Takeaway"
    PlAddress="Dunkellin Street, Loughrea"
    County="Galway"
    PhoneNumber="091 871551"/>
  <chipper
    LName="9th Lough"
    PlAddress="1 St Patricks Road, Clondalkin"
    County="Dunlin 22"
    PhoneNumber="01 4573267"/>
  <chipper
    LName="Aldo's Diner"
    PlAddress="Old Bray Road, Cornelscourt, Foxrock"
    County="Dunlin 18"
    PhoneNumber="01 2899226"/>
  <chipper
    LName="Alfredo’s Take Away"
    PlAddress="81 Macroom Road, Coolock"
    County="Dunlin 17"
    PhoneNumber="01 8474641"/>
</search>

This is my C# Code where i'm getting a the NullReferenceException the breakpoint appears where find.Attribute("County").Value == "Galway"?

 private void newsEventBtn_Click(object sender, RoutedEventArgs e)       
{

XDocument loadedData = XDocument.Load("SearchFile.xml");           
var filteredData = from find in loadedData.Descendants("search")                                                                            
         where find.Attribute("County").Value == "Galway"                             
 select new Chippers()                             
{

        LName = find.Attribute("LName").Value
        //PlAddress = c.Attribute("PAddress").Value,
        //PhoneNumber = c.Attribute("PhoneNumber").Value
};
  listBox.ItemsSource = filteredData;

This my chippers class Chippers.cs that im using store the xml elements values

public class Chippers
    {


        string name;
        string PAddress;
        string county;
        string phoneNumber;

        public string LName
        {
            get { return name; }
            set { name = value; }
        }

        public string PlAddress
        {
            get { return PAddress; }
            set { PAddress = value; }
        }

        public string County
        {
            get { return county; }
            set { county = value; }
        }
        public string PhoneNumber
        {
            get { return phoneNumber; }
            set { phoneNumber = value; }
        }

    }
}

if anyone could tell me why this is occurring it would be appreciate thanks

1 Answers1

0

you have to repeat over 'chipper' in stead of 'search'. Change:

var filteredData = from find in loadedData.Descendants("search")  

in:

var filteredData = from find in loadedData.Descendants("chipper")

Greets

Dusty
  • 140
  • 7