C# WinForms 读取 XML 文件 - 仅特定节点

标签 c# winforms readxml

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

目前我使用这个将其读入数据 GridView :

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 对象。有谁知道这样做的方法吗?目前我已经尝试了命名空间中包含的各种方法,但都无济于事。

帮助非常感谢,

问候。

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

最佳答案

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

关于C# WinForms 读取 XML 文件 - 仅特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1193555/

相关文章:

c# - EFCore 在 dbcontext 的生命周期内使用单个 dbconnect

winforms - Windows窗体-摆脱等待光标

c# - 如何使用 log4net 在日志文件中添加一个空行?

c# - 在 C# 中读取 XML 字符串

c# - 将 XML 读入字典

c# - 本地函数与 Lambda C# 7.0

c# - ng-grid 的水平滚动 - 2.0.11

c# - Windows 10 Enterprise Insider Preview 上 vs 2017 的错误文件图标

c# - 如何获取文本基线与标签水平边框之间的距离?

java - 如何使用 Java DOM 解析器读取 XML 字符串?民意调查、结果