2012-03-08 70 views
0

這是我SearchFile.xml,即時通訊使用由縣級過濾削片機WP7中使用XML:過濾的NullReferenceException是未處理的C#

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

這是我爲我的C#代碼得到一個NullReferenceException 斷點出現在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; 

這是我的削片機類Chippers.cs,即時通訊使用存儲XML元素的值

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

    } 
} 

如果有人能告訴我這是爲什麼發生這將是讚賞 謝謝

回答

0

你必須重複'削片'而不是'搜索'。變化:

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

在:

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

電賀

相關問題