这是我用来按县过滤削片机的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(“县”)。值==“戈尔韦”?
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 :(得分:0)
你必须重复'削片机'而不是'搜索'。变化:
var filteredData = from find in loadedData.Descendants("search")
在:
var filteredData = from find in loadedData.Descendants("chipper")
迎接