【问题标题】:C# WinForms Read XML File - Only Specific NodesC# WinForms 读取 XML 文件 - 仅特定节点
【发布时间】:2010-11-14 16:18:58
【问题描述】:

我有一个 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>

目前我使用以下方法将其读入数据网格视图:

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

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

帮助不胜感激,

问候。

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

【问题讨论】:

    标签: c# winforms readxml


    【解决方案1】:

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

    【讨论】:

    • +1 同意。除非数据太大,否则只需在 DataSet 中过滤即可。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      相关资源
      最近更新 更多