WP7 操作XML文件
public
partial
class MainPage : PhoneApplicationPage
{
XElement carMakers;
// Constructor
public MainPage()
{
InitializeComponent();
StreamResourceInfo xml = Application.GetResourceStream(new Uri("/WindowsPhoneLoadingXMLFileDemo;component/Carmakers.xml",
System.UriKind.Relative));
carMakers = XElement.Load(xml.Stream);
this.ManipulationStarted
+= new EventHandler<ManipulationStartedEventArgs>(MainPage_ManipulationStarted);
}
void MainPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
var carNames = from car
in carMakers.Elements("CarBrand")
select car.Attribute("Name").Value;
lbxCarmakers.ItemsSource = carNames;
}
}
另外,对于XML文件来说,也可以直接通过
XElement.Load("/WindowsPhoneLoadingXMLFileDemo;component/Carmakers.xml");
来获取到一个XElement。