이전 코드를 선택한 후 XML 파일에서 각 하위 요소의 속성을 읽도록하는 코드가 있습니다. 여기 내가 사용하여 XML입니다 - 그래서 기본적으로 프로그램에 ListBox1의 모든 모험 경로를 나열 어떻게해야하는지하위 하위에서 속성 가져 오기
<?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>
, 나는 중 하나는 모험 경로를 상기 선택합니다. 이 프로그램은 선택한 모험 경로에있는 모든 모험을 나열하고 하나를 선택합니다. 마지막으로 프로그램은 선택된 모험의 모든 시나리오를 나열합니다. 현재 두 번째 목록에서 완벽하게 수행 할 것이지만 두 번째 목록에서 모험을 선택하면 시나리오를 나열 할 수없는 것 같습니다. 충돌하지 않고 그냥 나열하지 않습니다. 어떤 도움이라도 좋을 것이고 여기에 모든 시나리오 목록을 작성해야하는 것에 대한 제 코드가 있습니다.
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);
}
}
}
(i는 변수를 오해하지 말아 경우)
오, 죄송합니다.이 사이트를 처음 사용합니다. – GiantDwarf
각 페이지 상단에 매우 유용한 "도움말"버튼이 있습니다. –