0

这是我用来按县过滤削片机的 SearchFile.xml

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

    }
}

如果有人能告诉我为什么会发生这种情况,将不胜感激谢谢

4

1 回答 1

0

您必须重复“削片机”而不是“搜索”。改变:

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

在:

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

问候

于 2012-03-09T13:45:37.677 回答