zoukankan      html  css  js  c++  java
  • Silverlight中DataDrid绑定XML数据

    Silverlight中DataDrid绑定XML数据,从老外的网站上转过来的,参考一下

    Bind a given DataGrid data source to a given XML file

    DataGrid could also be bound to a another persisted kind of datasoures, such as XML ressources or something spectial such as RSS feeds. To bind a given DataGrid to a given xml file data source, the following example is provided to illustrate that.

    Create a new Silverlight application



    Figure 1

    First, In the XAML code editor add the following code

           <Grid x:Name="LayoutRoot" Background="White">
                <data:DataGrid x:Name="myDataGrid"
                        AutoGenerateColumns="True"
                        >
                </data:DataGrid>
            </Grid>

    As you can remark, the DataGrid should be named so that it could be refered later in the C# code behind. Now, let consider this xml file as a main data source for our DataGrid.

    <?xml version="1.0" encoding="utf-8" ?>
    <myFamily>
    <
    item>
        <
    FirstName>Bejaoui</FirstName>
        <LastName>Habib</LastName>
        <Age>66</Age>
        <IsMale>true</IsMale>
    </item>
    <
    item>
        <
    FirstName>Ben Garga</FirstName>
        <LastName>Kadija</LastName>
        <Age>63</Age>
        <IsMale>false</IsMale>
    </item>

    <item>
        <
    FirstName>Bejaoui</FirstName>
        <LastName>Bechir</LastName>
        <Age>30</Age>
        <IsMale>true</IsMale>
    </item>
    <
    item>
        <
    FirstName>Bejaoui</FirstName>
        <LastName>Arbia</LastName>
        <Age>25</Age>
        <IsMale>false</IsMale>
    </item>
    <
    item>
        <
    FirstName>Bejaoui</FirstName>
        <LastName>Rim</LastName>
        <Age>20</Age>
        <IsMale>false</IsMale>
    </item>
    </
    myFamily>

    Second, a class that fits the xml structure should be developed. It could be represented as under

    public class Person
            {
                public string FirstName { get; set; }
                public string LastName { get; set; }
                public string Age { get; set; }
                public string IsMale { get; set; }
            }

    Third, a reference to the System.Xml.Linq namespace should be added



    Figure 2

    In the code behind, a DataGrid object should be declared and the under code should be implemented

         public partial class Page : UserControl
            {
                DataGrid oGrid;
                public Page()
                {
                    InitializeComponent();
                    InitializeGrid();
                }
                public void InitializeGrid()
                {
                    XDocument oDoc = XDocument.Load("File.xml");
                    var myData = from info in oDoc.Descendants("item")
                                 select new Person
                                 {
                                     FirstName = Convert.ToString(info.Element("FirstName").Value),
                                     LastName = Convert.ToString(info.Element("LastName").Value),
                                     Age = Convert.ToString(info.Element("Age").Value),
                                     IsMale = Convert.ToString(info.Element("IsMale").Value)
                                 };
                    oGrid = this.FindName("myDataGrid") as DataGrid;
                    oGrid.ItemsSource = myData;

                 }
            }

    This leads to the following result


  • 相关阅读:
    初学oracle,创建数据库时出现ora00988:缺少或无效口令 以及登陆
    NUnit单元测试笔记
    请给Array本地对象增加一个原型方法,它的用途是删除数组条目中重复的条目(可能有多个),返回值是一个仅包含被删除的重复条目的新数组。
    给iframe添加onload事件
    给超链接去焦点框(虚线框)时遇到的问题
    FCKeditor上传文件重命名for php
    PDF Xchange Pro 3.6的注册码
    APNG
    一个as3的alert
    jQuery遮罩弹窗
  • 原文地址:https://www.cnblogs.com/zhangq723/p/1707234.html
Copyright © 2011-2022 走看看