2009-07-28 48 views
1

我有一个XML文件:C#的WinForms读取XML文件 - 只有特定的节点

<Database> 
    <SMS> 
    <Number>+447761692278</Number> 
    <DateTime>2009-07-27T15:20:32</DateTime> 
    <Message>Yes</Message> 
    <FollowedUpBy>Unassigned</FollowedUpBy> 
    <Outcome></Outcome> 
    <Quantity>0</Quantity> 
    <Points>0</Points> 
    </SMS> 
    <SMS> 
    <Number>+447706583066</Number> 
    <DateTime>2009-07-27T15:19:16</DateTime> 
    <Message>STOP</Message> 
    <FollowedUpBy>Unassigned</FollowedUpBy> 
    <Outcome></Outcome> 
    <Quantity>0</Quantity> 
    <Points>0</Points> 
    </SMS> 
    </Database> 

目前我使用这个读入一个DataGridView:

public void Read() 
     { 
      DataSet ds = new DataSet("SMS DataSet"); 
      XmlDataDocument xmlDatadoc = new XmlDataDocument(); 
      xmlDatadoc.DataSet.ReadXml(@"C:\Documents and Settings\Administrator\Desktop\RecSmsDB.xml"); 
      ds = xmlDatadoc.DataSet; 
      this.dataGridView1.DataSource = ds; 
      this.dataGridView1.DataMember = "SMS"; 
      this.dataGridView1.Sort(dataGridView1.Columns["DateTime"], ListSortDirection.Descending); 
     } 

我希望能够为只读在具有特定DateTime的xml对象中。有谁知道这样做的一种方式?目前我已经尝试了命名空间中包含的各种方法,但无济于事。

非常感谢,

关于。

***编辑:我希望能够动态地改变运行时显示的数据。

回答

4

不是我能想到的。但是,您可以将所有数据读入DataSet,然后创建一个DataView来过滤SMS表,并将您的网格绑定到DataSet而不是DataTable。

+0

+1同意。除非数据过大,否则只需在DataSet中对其进行过滤。 – 2009-07-28 11:56:13

1

如果将DataSet附加到BindingSource并将此BindingSource附加到网格会怎么样? BindingSource有一个Filter属性,您可以在其中输入类似SQL的表达式。