WP7的XML操作详解(转)
2012-06-26 10:00
120 查看
WP7 xml操作
在这个小教程,我将演示在Windows Phone 7如何让ListBox的数据绑定XML数据。我将使用LINQ to XML,以便加载和读取数据,而且我将展示如何实现一个基本的过滤。 首先让我们先创建一个Windows Phone 7的应用程序项目示例,并添加以下两个demo xml文件。 people.xml
<?xml version="1.0" encoding="utf-8" ?>
<people>
<person>
<firstname>Kate</firstname>
<lastname>Smith</lastname>
<age>27</age>
</person>
<person>
<firstname>Tom</firstname>
<lastname>Brown</lastname>
<age>30</age>
</person>
<person>
<firstname>Tim</firstname>
<lastname>Stone</lastname>
<age>36</age>
</person>
<person>
<firstname>Ann</firstname>
<lastname>Peterson</lastname>
<age>27</age>
</person>
</people>
复制代码
PeopleCustom.xml
<?xml version="1.0" ?>
<People>
<Person
FirstName="Kate"
LastName="Smith"
Age="27" />
<Person
FirstName="Tom"
LastName="Brown"
Age="30" />
<Person
FirstName="Tim"
LastName="Stone"
Age="36" />
<Person
FirstName="Ann"
LastName="Peterson"
Age="27" />
</People>
复制代码
下一步是创建一个示例类将被用来存储XML元素值:
public class Person
{
string firstname;
string lastname;
int age;
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
复制代码
您所在的用户组无法下载或查看附件为了读取XML文件的信息,我们将使用的XDocument 所以你首先需要添加System.Xml.Linq.dll引用,然后using System.Xml.Linq;
XDocument loadedData = XDocument.Load("People.xml");
var data = from query in loadedData.Descendants("person")
select new Person
{
FirstName = (string)query.Element("firstname"),
LastName = (string)query.Element("lastname"),
Age = (int)query.Element("age")
};
listBox.ItemsSource = data;
复制代码
在接下来的例子中,我们将通过数据的“年龄”属性值过滤。源代码如下:
XDocument loadedCustomData = XDocument.Load("PeopleCustom.xml");
var filteredData = from c in loadedCustomData.Descendants("Person")
where c.Attribute("Age").Value == "27"
select new Person()
{
FirstName = c.Attribute("FirstName").Value,
LastName = c.Attribute("LastName").Value
};
listBox1.ItemsSource = filteredData;
复制代码
为了显示的数据,我们将使用以下ItemTemplates绑定ListBox控件:
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Orientation="Horizontal">
<TextBlock Text="XML Data:"/>
<ListBox x:Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" >
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text="{Binding Age}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Text="Filtered by Age 27:"/>
<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="20" >
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
复制代码
在这个小教程,我将演示在Windows Phone 7如何让ListBox的数据绑定XML数据。我将使用LINQ to XML,以便加载和读取数据,而且我将展示如何实现一个基本的过滤。 首先让我们先创建一个Windows Phone 7的应用程序项目示例,并添加以下两个demo xml文件。 people.xml
<?xml version="1.0" encoding="utf-8" ?>
<people>
<person>
<firstname>Kate</firstname>
<lastname>Smith</lastname>
<age>27</age>
</person>
<person>
<firstname>Tom</firstname>
<lastname>Brown</lastname>
<age>30</age>
</person>
<person>
<firstname>Tim</firstname>
<lastname>Stone</lastname>
<age>36</age>
</person>
<person>
<firstname>Ann</firstname>
<lastname>Peterson</lastname>
<age>27</age>
</person>
</people>
复制代码
PeopleCustom.xml
<?xml version="1.0" ?>
<People>
<Person
FirstName="Kate"
LastName="Smith"
Age="27" />
<Person
FirstName="Tom"
LastName="Brown"
Age="30" />
<Person
FirstName="Tim"
LastName="Stone"
Age="36" />
<Person
FirstName="Ann"
LastName="Peterson"
Age="27" />
</People>
复制代码
下一步是创建一个示例类将被用来存储XML元素值:
public class Person
{
string firstname;
string lastname;
int age;
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
复制代码
您所在的用户组无法下载或查看附件为了读取XML文件的信息,我们将使用的XDocument 所以你首先需要添加System.Xml.Linq.dll引用,然后using System.Xml.Linq;
XDocument loadedData = XDocument.Load("People.xml");
var data = from query in loadedData.Descendants("person")
select new Person
{
FirstName = (string)query.Element("firstname"),
LastName = (string)query.Element("lastname"),
Age = (int)query.Element("age")
};
listBox.ItemsSource = data;
复制代码
在接下来的例子中,我们将通过数据的“年龄”属性值过滤。源代码如下:
XDocument loadedCustomData = XDocument.Load("PeopleCustom.xml");
var filteredData = from c in loadedCustomData.Descendants("Person")
where c.Attribute("Age").Value == "27"
select new Person()
{
FirstName = c.Attribute("FirstName").Value,
LastName = c.Attribute("LastName").Value
};
listBox1.ItemsSource = filteredData;
复制代码
为了显示的数据,我们将使用以下ItemTemplates绑定ListBox控件:
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Orientation="Horizontal">
<TextBlock Text="XML Data:"/>
<ListBox x:Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" >
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text="{Binding Age}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Text="Filtered by Age 27:"/>
<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="20" >
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
复制代码
相关文章推荐
- WP7 XML操作详解:读取,过滤以及数据绑定
- WP7的XML操作详解:读取,过滤以及数据绑定
- WP7的XML操作详解:读取,过滤以及数据绑定
- java中四种操作(DOM、SAX、JDOM、DOM4J)xml方式的比较与详解(code稍作修改)
- java中四种操作(DOM、SAX、JDOM、DOM4J)xml方式详解与比较
- C#操作XML方法详解
- java操作(DOM、SAX、JDOM、DOM4J)xml方式的四种比较与详解
- java中四种操作(DOM、SAX、JDOM、DOM4J)xml方式详解与比较
- java中四种操作(dom、sax、jdom、dom4j)xml方式详解与比较
- C#操作XML方法详解
- java中四种操作(DOM、SAX、JDOM、DOM4J)xml方式详解与比较(二)
- ActionScript 3操作XML 详解
- SQL Server 2005中XML操作函数详解
- wp7 操作xml
- 详解PHP原生DOM对象操作XML的方法
- 详解PHP原生DOM对象操作XML的方法
- 基于PHP对XML的操作详解
- Android利用SAX对XML进行增删改查操作详解
- java中四种操作(dom、sax、jdom、dom4j)xml方式详解与比较
- Android利用Dom对XML进行增删改查操作详解