我无法获得显示结果的列表框,我只能使用“nameInput.Text”,我不知道如何在foreach中工作,任何人都可以帮忙吗?
C#代码:
XDocument xdoc = XDocument.Load("biblia.xml");
var pais =
from pai in xdoc.Descendants("b")
where (string)pai.Attribute("indice") == selectedindex.ToString()
select new
{
Filho = pai.Descendants("c")
};
foreach (var pai in pais) {
foreach (var filho in pai.Filho) {
nameInput.Text = nameInput.Text + " " + filho.Attribute("n");
}
}
XAML:
<ListBox x:Name="listBox2">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" >
<TextBlock Text="{Binding filho}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
编辑:这是xml的一部分
XML:
<?xml version="1.0" encoding="UTF-8"?>
<bible>
<b n="Gênesis" indice="1">
<c n="1">
<v n="1"></v>
<v n="2"></v>
</c>
<c n="2">
<v n="1"></v>
<v n="2"></v>
</c>
<b n="Êxodo" indice="2">
<c n="1">
<v n="1"></v>
<v n="2"></v>
</c>
<c n="2">
<v n="1"></v>
<v n="2"></v>
</c>
</b>
</bible>
编辑 我认为解决方案是创建一个List:
List<string> MyList = new List<string>();
foreach (var pai in pais) {
foreach (var filho in pai.Filho) {
MyList.Add(filho.Attribute("n").Value);
}
}
listBoxTeste1.ItemsSource = MyList;