Monday, August 26, 2013

Ho to use XML files In WinPhone

If we are storing static data in our Win Phone app, xml files can be used  for that.
Following sample explains how to do that:
<CountrList>
  <country name="Abkhazia" capital ="Sukhumi" />
  <country name="Afghanistan" capital ="Kabul" />
  <country name="Albania" capital ="Tirana"  />
</CountrList>


we can bind xml data to a list by using following code
<ListBox Name="lstData"
         ItemsSource="{Binding}"
         DisplayMemberPath="objMyList" />


public class clsWorldList
{
  public string MyName { get; set; }
  public string MyCapital { get; set; }
}
....
....
var xElem = XElement.Load("XML/List.xml");
  var objMyList =
      from elem in xElem.Descendants("country")
      orderby elem.Attribute("name").Value

      select new clsWorldList
       {
         MyName = elem.Attribute("name").Value,
         MyCapital = elem.Attribute("capital").Value,
       };

  lstData.DataContext = objMyList ;


No comments:

Post a Comment

How to upload app to macOS

1. Open Terminal Press Cmd (⌘) + Space , type Terminal , and hit Enter . 2. Navigate to Your Build Output Directory Your .app file is likel...