简体   繁体   中英

C# WinForms Read XML File - Only Specific Nodes

I have an XML File:

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

Currently I read it into a datagridview using this:

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

I want to be able to only read in the xml objects that have a specific DateTime. Does anyone know of a way of doing this? Currently I have tried a variety of the methods included in the namespace but to no avail.

Help greatly appreciated,

regards.

***EDIT: I want to be able to dynamically change the data displayed at run time.

Not that I can think of. However, you can read all the data into the DataSet, then create a DataView which filters the SMS table, and bind your grid to that instead of the DataTable.

What if you attach the DataSet to a BindingSource and attach this BindingSource to the grid? A BindingSource has a Filter property where you can enter SQL-like expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM