したがって、前の要素を選択した後、xml ファイルに各子孫要素の属性を読み込ませることになっているこのコードがあります。これが私が使用しているxmlです-
<?xml version="1.0" encoding="utf-8" ?>
<adventures>
<adventure_path Name ="Adventure Path 1">
<adventure Name ="Adventure 1">
<senario Name ="Senario 1">
<location Name="Location 1" Players="1"/>
<location Name="Location 2" Players="1"/>
</scenario>
<senario Name ="Senario 2">
<location Name="Location 3" Players="1"/>
<location Name="Location 4" Players="1"/>
</scenario>
</adventure>
<adventure Name="Addventure 2">
<senario Name ="Senario 3">
<location Name="Location 5" Players="1"/>
<location Name="Location 6" Players="1"/>
</scenario>
</adventure>
</adventure_path>
<adventure_path Name ="Adventure Path 2">
<adventure Name ="Adventure 3">
<senario Name ="Senario 4">
<location Name="Location 7" Players="1"/>
<location Name="Location 8" Players="1"/>
</scenario>
<senario Name ="Senario 5">
<location Name="Location 9" Players="1"/>
<location Name="Location 10" Players="1"/>
</scenario>
</adventure>
</adventure_path>
</adventures>
基本的に、プログラムはリストボックス 1 にすべてのアドベンチャー パスをリストします。そのアドベンチャー パスの 1 つを選択します。プログラムは、選択されたアドベンチャー パス内のすべてのアドベンチャーを一覧表示します。私は 1 つを選択します。最後に、プログラムは選択した冒険のすべてのシナリオを一覧表示します。現在起こっていることは、最初の 2 つのリストは完全に実行されますが、2 番目のリストで冒険を選択すると、シナリオのリストを取得できないようです. クラッシュすることはありません。リストに表示されないだけです。すべてのシナリオのリストを作成するための私のコードは次のとおりです。
private void lst_Adventures_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedItem = lst_Adventure.SelectedItem.ToString();
string selectedAdventure = lst_Adventures.SelectedItem.ToString();
lst_Senarios.Items.Clear();
System.Console.WriteLine(selectedItem);
XDocument doc = new XDocument();
doc = XDocument.Load("D:\\WpfApplication1\\WpfApplication1\\Adventures.xml");
XElement selectedElement = doc.Descendants().Where(x => (string)x.Attribute("Name") == selectedItem).FirstOrDefault();
XElement selectedAdventures = selectedElement.Descendants().Where(x => (string)x.Attribute("Name") == selectedItem).FirstOrDefault();
if (selectedAdventures != null)
{
foreach (var docs in selectedAdventures.Elements("senario"))
{
string AdventuresPathName = docs.Attribute("Name").Value;
lst_Adventures.Items.Add(AdventuresPathName);
}
}
}