2012-04-04 5 views
0

실버 라이트로 XML 파일에서 국가 이름과 도시를로드하려고합니다.WP7에서이 XML 파일을 읽으려면 어떻게해야합니까?

도시를 선택한 국가에 따라 ListPicker에로드하려고합니다.

이 내 XML 파일의 일부입니다

<Times> 
    <country name="USA"> 
     <city name="Aaronsburg -- PA"> 
      <state>PA</state> 
      <latitude>408739</latitude> 
      <longitude>-773815</longitude> 
      <timezone>-500</timezone> 
      <daylight>1</daylight> 
     </city> 
     <city name="Abbeville -- AL"> 
      <state>AL</state> 
      <latitude>316077</latitude> 
      <longitude>-853051</longitude> 
      <timezone>-600</timezone> 
      <daylight>1</daylight> 
     </city> 
     <city name="Abbeville -- GA"> 
      <state>GA</state> 
      <latitude>319710</latitude> 
      <longitude>-833016</longitude> 
      <timezone>-500</timezone> 
      <daylight>1</daylight> 
     </city> 
     <city name="Abbot -- ME"> 
      <state>ME</state> 
      <latitude>453219</latitude> 
      <longitude>-695342</longitude> 
      <timezone>-500</timezone> 
      <daylight>1</daylight> 
      </city> 
........ 
........ 

이 코드는 내가 쓴 :

private void LoadButton_Click(object sender, RoutedEventArgs e) 
{ 
    XDocument doc = XDocument.Load("Athan.xml"); 
    var definitions = doc.Document.Descendants(XName.Get("country")); 

    foreach (var definition in definitions) 
    { 
     if (definition.Attribute(XName.Get("name")).Value == CountryListPicker.SelectedItem.ToString()) 
     { 
      var cities = definition.Document.Descendants(XName.Get("city")); 

      foreach (var city in cities) 
      { 
       CityListPicker.Items.Add(city.Attribute(XName.Get("name")).Value.ToString()); 
      } 

      return; 
     } 
    } 
} 

도시 오랜만에로드 또는로드되지 않습니다! 내 코드에 문제가 있습니까?

답변

0

좋아요, 나는 windows-phone-7에 대해 아무 것도 모르기 때문에 여기에서 완전히 벗어날 수도 있지만, LoadButton_Click 메서드의 핵심은 Linq에서 Xml이라고 가정합니다.

그렇다면 .Document에 대한 호출과 XName에 대한 자주 호출에 대해 궁금합니다. 예를 들어, 정의에 대한 코드는 "var definitions = doc.Descendants ("country ");와 같이 간단 할 수 있습니다. 'var cities'행과 유사합니다.

속성을 처리 할 때 "CityListPicker.Items.Add (city.Attribute ("name "))와 같은 값을 얻을 수 있습니다.

데이터가 적어도 일부 시간로드되기 때문에 네임 스페이스 문제가없는 것으로 간주되지만 확인해야합니다.

희망이 도움이됩니다.

+0

감사를

XDocument doc = XDocument.Load("Athan.xml"); XElement root = doc.Root; foreach (XElement el in root.Descendants("Times")) { // code to navigate in your XML if (el.Name == "country"){ foreach (XAttribute attr in el.Attributes()) { if (attr.name == "name"){ // receive your country name String CountryName = attr.Value; } } foreach (XElement city in el.Descendants()) { if (city.Name == "city") { Object City = new Object(); // create your object... foreach (XAttribute attr in el.Attributes()) { if (attr.name == "state"){ City.sate = cityAttr.Value; } if (attr.name == "latitude"){ City.latitude = cityAttr.Value; } // etc.... } //You add your object city in list (for example) ... } } 

을 추가 할 수 help, system.xml.linq를 사용하고 있습니다. 코드가 모든 데이터를 메모리로 가져오고, 응용 프로그램이 멈추었을 것입니다. 거기에 메모리에 완전히로드하지 않고 XML 파일을 읽을 수있는 방법이 있다고 생각합니다. 감사합니다 –

+0

메모리에 파일을로드하는 데 문제가있는 경우이 링크가 도움이 될 수 있습니다. http://stackoverflow.com/questions/5838657/how-can-i-use-linq-to-xml-to-query-huge -xml-files-with-reasonable-memory-consump – Steve

0

시도 ... 내 XML을로드하는 데 문제가 있고,이 방법으로, XML은로드에 대한

희망이 솔루션의 도움 당신은 ...

관련 문제