2013-12-08 3 views
0

이전 코드를 선택한 후 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); 
     } 
    } 
} 
+0

(i는 변수를 오해하지 말아 경우)

  • selectedAdventures 대신하여 selectedItem의 selectedAdventure에 대해 일치해야합니다. "[제목에"태그 "가 포함되어 있어야합니까?] (http://meta.stackexchange.com/questions/19190/)"합의가 "아니오, 그렇지 않아야한다"는 것을 참조하십시오. –

  • +0

    오, 죄송합니다.이 사이트를 처음 사용합니다. – GiantDwarf

    +0

    각 페이지 상단에 매우 유용한 "도움말"버튼이 있습니다. –

    답변

    0

    제공하신 xml로 코드를 시험해 보았습니다. 몇 가지 결함이 있습니다.

    1. 열기 태그를 senario에 대한 닫는 태그 scenario 일치 나던 : (난 당신이 무엇을 기대 오해 해달라고하면) 그 예상대로 코드가 실행됩니다 올바르게 수정합니다. 오프닝 태그를 scenario으로 변경하면 foreach에서 "senario"를 "scenario"로 변경하는 것을 잊지 마십시오. 내가 제목을 편집 한
    +0

    이 작품 이니, 없습니까? – har07