Detailed description of WP7 XML operations: Reading, filtering, and Data Binding

Source: Internet
Author: User

In this tutorial, I will demonstrate how to bind ListBox data to XML data on Windows Phone 7. I will use LINQ to XML to load and read data, and I will demonstrate how to implement a basic filter.

First, let's create a Windows Phone 7 Application project example and add the following two demo xml files.

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>


Here, I have to thank the author who has always supported me for writing such an article with interest. I would like to thank the author again for a very good wp7 Development Forum, I will also post several high-quality articles to you later. Please contact me on the noodles.

Enter the subject:

 

 


Lelecustom. 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>

The next step is to create an example class to store XML element values:

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; }
}
}

To read the XML file information, we will use the XDocument

So you need to addSystem. Xml. Linq. dllReference, and then usingSystem. 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;

In the following example, we will use the "Age" attribute value of the dataFilter. The source code is as follows:

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;

To display the data, we will use the following ItemTemplates to bind the ListBox control:

<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>

I hope you like my article! If you have more ideas, please contact me in the Q & a area of the wp7 Development Forum (codewp7.com). I will be glad to know what you are thinking. At the same time, I can also find my figure in wp7 chat QQ Group 172765887. Thank you!

Please slam the source code


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.