Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Removing rows depending on date [modified]

Removing rows depending on date [modified]

Scheduled Pinned Locked Moved C#
xmldatabasehelpquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, Im trying to remove a whole table from an xml file if the date is a month old or older. This is the code i produced so far: using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Xml; using System.IO; using System.Xml.Xsl; using System.Xml.Schema; using System.IO.Compression; namespace RemoveRows { class Program { static void Main(string[] args) { try { DataSet xmlFile = new DataSet(); xmlFile.ReadXml("xmlFile.xml"); foreach (DataRow dateRow in xmlFile.Tables[0].Rows) { string Date = dateRow["Date"].ToString(); DataRow[] tempXmlFileRows = xmlFile.Tables[0].Select("Date = " + Date); DateTime dateNow = DateTime.Now; DateTime dateOld = string dateFormat = @"yyyy/MM/Dd"; } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); } } } And this is how the xmlFile looks like: 1 20060604 1200 The first 4 digits are presenting the year, the next 2 digits the month and the last 2 the day. Can somebody help me with this please? Thanks in advance! -- modified at 14:38 Thursday 8th June, 2006

    S 1 Reply Last reply
    0
    • Y Yustme

      Hi, Im trying to remove a whole table from an xml file if the date is a month old or older. This is the code i produced so far: using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Xml; using System.IO; using System.Xml.Xsl; using System.Xml.Schema; using System.IO.Compression; namespace RemoveRows { class Program { static void Main(string[] args) { try { DataSet xmlFile = new DataSet(); xmlFile.ReadXml("xmlFile.xml"); foreach (DataRow dateRow in xmlFile.Tables[0].Rows) { string Date = dateRow["Date"].ToString(); DataRow[] tempXmlFileRows = xmlFile.Tables[0].Select("Date = " + Date); DateTime dateNow = DateTime.Now; DateTime dateOld = string dateFormat = @"yyyy/MM/Dd"; } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); } } } And this is how the xmlFile looks like: 1 20060604 1200 The first 4 digits are presenting the year, the next 2 digits the month and the last 2 the day. Can somebody help me with this please? Thanks in advance! -- modified at 14:38 Thursday 8th June, 2006

      S Offline
      S Offline
      Stephan Samuel
      wrote on last edited by
      #2

      You're on the right track. You've loaded the data and you're iterating suitably. What you need now is to find the date to cut off at, and start removing records. To find the day that was 30 days before today, try this: DateTime dtOld = DateTime.Now.Date.Subtract(new TimeSpan(30, 0, 0, 0)); Unfortunately, and for good reason, there's no easy constructor for TimeSpan to put in the month value and go. You could find it iterativel, something like this: DateTime dtOld = DateTime.Now.Date; while (dtOld.AddMonths(1).Date > DateTime.Now.Date) dtOld = dtOld.Subtract(new TimeSpan(1, 0, 0, 0)); dtOld will contain one month before today when that's running. Next, you need an int value, because the <Date /> value stored in your XML is really just an int. Use this: int intOld = dtOld.ToString("yyyyMMdd"); Now, as you iterate through your set, compare each value to that value. If the int is less than that number, get rid of the data element. HTH, ask more if you need more help.

      Y 1 Reply Last reply
      0
      • S Stephan Samuel

        You're on the right track. You've loaded the data and you're iterating suitably. What you need now is to find the date to cut off at, and start removing records. To find the day that was 30 days before today, try this: DateTime dtOld = DateTime.Now.Date.Subtract(new TimeSpan(30, 0, 0, 0)); Unfortunately, and for good reason, there's no easy constructor for TimeSpan to put in the month value and go. You could find it iterativel, something like this: DateTime dtOld = DateTime.Now.Date; while (dtOld.AddMonths(1).Date > DateTime.Now.Date) dtOld = dtOld.Subtract(new TimeSpan(1, 0, 0, 0)); dtOld will contain one month before today when that's running. Next, you need an int value, because the <Date /> value stored in your XML is really just an int. Use this: int intOld = dtOld.ToString("yyyyMMdd"); Now, as you iterate through your set, compare each value to that value. If the int is less than that number, get rid of the data element. HTH, ask more if you need more help.

        Y Offline
        Y Offline
        Yustme
        wrote on last edited by
        #3

        Hi Stephan Samuel, Thank you for your reply! Im getting an error coz of this line: int intOld = dtOld.ToString("yyyyMMdd"); It says: Cannot implicitly convert type 'string' to 'int'. And ehm it should not only remove the tag '', but the whole table which contains the outdated row. This is kinda tricky for me. Thank you very much!

        S 1 Reply Last reply
        0
        • Y Yustme

          Hi Stephan Samuel, Thank you for your reply! Im getting an error coz of this line: int intOld = dtOld.ToString("yyyyMMdd"); It says: Cannot implicitly convert type 'string' to 'int'. And ehm it should not only remove the tag '', but the whole table which contains the outdated row. This is kinda tricky for me. Thank you very much!

          S Offline
          S Offline
          Stephan Samuel
          wrote on last edited by
          #4

          Oh, yeah... this'll work better: int intOld = Int32.Parse(dtOld.ToString("yyyyMMdd")); Removing elements from a DataSet is pretty easy: DataSet dsMyData; // Populate DataSet. ... // Remove a table by name. dsMyData.Tables.Remove("MyTableName"); // Remove a table by index. dsMyData.Tables.RemoveAt(0); DataRow dtMyRow = dsMyData.Tables["MyOtherTable"].Rows[0]; // Remove a row by reference. dsMyData.Tables["MyOtherTable"].Rows.Remove(dtMyRow); // Remove a row by index. dsMyData.Tables["MyOtherTable"].Rows.RemoveAt(0); // Make sure to save changes. dsMyData.AcceptChanges(); dsMyData.WriteXml("c:\myfile.xml"); There are other issues with deleting data from a DataSet, but you may get lucky and avoid the bulk of them. It's worth a try, and ask if you're getting a particular error you can't get through. Stephan

          Y 1 Reply Last reply
          0
          • S Stephan Samuel

            Oh, yeah... this'll work better: int intOld = Int32.Parse(dtOld.ToString("yyyyMMdd")); Removing elements from a DataSet is pretty easy: DataSet dsMyData; // Populate DataSet. ... // Remove a table by name. dsMyData.Tables.Remove("MyTableName"); // Remove a table by index. dsMyData.Tables.RemoveAt(0); DataRow dtMyRow = dsMyData.Tables["MyOtherTable"].Rows[0]; // Remove a row by reference. dsMyData.Tables["MyOtherTable"].Rows.Remove(dtMyRow); // Remove a row by index. dsMyData.Tables["MyOtherTable"].Rows.RemoveAt(0); // Make sure to save changes. dsMyData.AcceptChanges(); dsMyData.WriteXml("c:\myfile.xml"); There are other issues with deleting data from a DataSet, but you may get lucky and avoid the bulk of them. It's worth a try, and ask if you're getting a particular error you can't get through. Stephan

            Y Offline
            Y Offline
            Yustme
            wrote on last edited by
            #5

            Hi Stephan, Thanks again for your reply. Im getting a really strange error about 'Table test does not belong to this DataSet.' from the console. I googled up this error, but no information what tells me how to deal with this error. This is the code i have produced so far (with help from you): static void Main(string[] args) { try { DataSet xmlFile = new DataSet(); xmlFile.ReadXml("test.xml"); foreach (DataRow dateRow in xmlFile.Tables[0].Rows) { string Date = dateRow["Date"].ToString(); //DataRow[] tempXmlFileRows = xmlFile.Tables[0].Select("Date = " + Date); //DateTime dateNow = DateTime.Now; DateTime dateOld = DateTime.Now.Date.Subtract(new TimeSpan(30, 0, 0, 0)); int intOld = Int32.Parse(dateOld.ToString("yyyyMMdd")); DateTime dtOld = DateTime.Now.Date; while (dateOld.AddMonths(1).Date > DateTime.Now.Date) { dateOld = dateOld.Subtract(new TimeSpan(1, 0, 0, 0)); if( tempXmlFileRows.Length != 0) xmlFile.Tables.Remove("test"); xmlFile.AcceptChanges(); } } xmlFile.WriteXml("test.xml"); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); } I cant use the DataRow[] array, coz if i do it says: Cannot find table 0. In the console. Thanks again!

            S 1 Reply Last reply
            0
            • Y Yustme

              Hi Stephan, Thanks again for your reply. Im getting a really strange error about 'Table test does not belong to this DataSet.' from the console. I googled up this error, but no information what tells me how to deal with this error. This is the code i have produced so far (with help from you): static void Main(string[] args) { try { DataSet xmlFile = new DataSet(); xmlFile.ReadXml("test.xml"); foreach (DataRow dateRow in xmlFile.Tables[0].Rows) { string Date = dateRow["Date"].ToString(); //DataRow[] tempXmlFileRows = xmlFile.Tables[0].Select("Date = " + Date); //DateTime dateNow = DateTime.Now; DateTime dateOld = DateTime.Now.Date.Subtract(new TimeSpan(30, 0, 0, 0)); int intOld = Int32.Parse(dateOld.ToString("yyyyMMdd")); DateTime dtOld = DateTime.Now.Date; while (dateOld.AddMonths(1).Date > DateTime.Now.Date) { dateOld = dateOld.Subtract(new TimeSpan(1, 0, 0, 0)); if( tempXmlFileRows.Length != 0) xmlFile.Tables.Remove("test"); xmlFile.AcceptChanges(); } } xmlFile.WriteXml("test.xml"); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); } I cant use the DataRow[] array, coz if i do it says: Cannot find table 0. In the console. Thanks again!

              S Offline
              S Offline
              Stephan Samuel
              wrote on last edited by
              #6

              Unfortunately (for you), I can't write your program for you. I recommend getting a book on C# programming that includes a few chapters on DataSets and XML, and learning from that. The errors your getting tell you exactly what's wrong. The tables you're trying to reference aren't in your DataSet. You need to point to the tables that you want to delete by their names or indices. Somehow or other, and I'll leave it for you to figure out how, you're going to have to inspect the DataSet schema that's created from ReadXml() on your XML listing. Here's a hint: if you're using VS.NET, run your app in the debugger and pay attention to the Autos, Locals, and Command windows. They'll give you a look into the DataSet, and you'll have an idea of what to delete.

              Y 1 Reply Last reply
              0
              • S Stephan Samuel

                Unfortunately (for you), I can't write your program for you. I recommend getting a book on C# programming that includes a few chapters on DataSets and XML, and learning from that. The errors your getting tell you exactly what's wrong. The tables you're trying to reference aren't in your DataSet. You need to point to the tables that you want to delete by their names or indices. Somehow or other, and I'll leave it for you to figure out how, you're going to have to inspect the DataSet schema that's created from ReadXml() on your XML listing. Here's a hint: if you're using VS.NET, run your app in the debugger and pay attention to the Autos, Locals, and Command windows. They'll give you a look into the DataSet, and you'll have an idea of what to delete.

                Y Offline
                Y Offline
                Yustme
                wrote on last edited by
                #7

                Hi Stephan, Thank you for your reply. The error did exactly tell me whats wrong, but the tables im trying to reference are in my dataset. So i tried deleting the table test. That is a table right?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups