Removing rows depending on date [modified]
-
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 -
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, 2006You'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. -
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.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!
-
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!
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 -
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. StephanHi 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!
-
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!
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.
-
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.