|
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:
<code>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();
}
}
}
</code>
And this is how the xmlFile looks like:
<root>
<test>
<Id> 1 </Id>
<Date> 20060604 </Date>
<Time> 1200 </Time>
</test>
</root>
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
|
|
|
|
|
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:
<br />
DateTime dtOld = DateTime.Now.Date.Subtract(new TimeSpan(30, 0, 0, 0));<br />
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:
<br />
DateTime dtOld = DateTime.Now.Date;<br />
while (dtOld.AddMonths(1).Date > DateTime.Now.Date)<br />
dtOld = dtOld.Subtract(new TimeSpan(1, 0, 0, 0));<br />
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:
<br />
int intOld = dtOld.ToString("yyyyMMdd");<br />
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 '<Date></Date>', 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:
<br />
int intOld = Int32.Parse(dtOld.ToString("yyyyMMdd"));<br />
Removing elements from a DataSet is pretty easy:
<br />
DataSet dsMyData;<br />
<br />
<br />
...<br />
<br />
dsMyData.Tables.Remove("MyTableName");<br />
dsMyData.Tables.RemoveAt(0);<br />
<br />
DataRow dtMyRow = dsMyData.Tables["MyOtherTable"].Rows[0];<br />
<br />
dsMyData.Tables["MyOtherTable"].Rows.Remove(dtMyRow);<br />
dsMyData.Tables["MyOtherTable"].Rows.RemoveAt(0);<br />
<br />
dsMyData.AcceptChanges();<br />
<br />
dsMyData.WriteXml("c:\myfile.xml");<br />
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
|
|
|
|
|
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.
|
|
|
|
|
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.
<root>
<test>
<id> </id>
<date> </date>
</test>
</root>
So i tried deleting the table test. That is a table right?
|
|
|
|
|
I'm new to C# (using 2003 .NET) and I was trying to create my first user control. Inside the User Control I have a toolbar which I would like to dock on the left. When I do so, the images inside my toolbar do not show, but the text does. I inserted the images by creating an imageList, which I reference in my toolbar and index on each of the buttons. I can see the image on the design view, but they never show when I run. I can choose to dock the toolbar on top, or on the bottom and then both images and text show. I would appreciate any ideas.
|
|
|
|
|
I'm not 100% sure, but I recall this might be a known bug. Try search the MSDN for details.
|
|
|
|
|
Using VS2005 and winforms. Have a TabControl with five TabPages. One page consists of labels and textboxes only. My users want the ability to print this TabPage. Not familiar with printing. Any easy way of doing this? thanks, gary
|
|
|
|
|
You could either write your own paint routine, or you can do the equivelant of a print screen by copying bits of the screen to a bitmap through API.
This is some VB code that I used in one of my other applications:
Public Function WindowCapture(ByVal form As Form) As Bitmap<br />
' Get this form's Graphics object.<br />
Dim me_gr As Graphics = form.CreateGraphics<br />
<br />
' Make a Bitmap to hold the image.<br />
Dim bm As New Bitmap(form.Width, form.Height, me_gr)<br />
Dim bm_gr As Graphics = me_gr.FromImage(bm)<br />
Dim bm_hdc As IntPtr = bm_gr.GetHdc<br />
<br />
' Get the form's hDC. We must do this after <br />
' creating the new Bitmap, which uses me_gr.<br />
Dim me_hdc As IntPtr = win32api.GetWindowDC(form.Handle)<br />
<br />
' BitBlt the form's image onto the Bitmap.<br />
win32api.BitBlt(bm_hdc, 0, 0, form.Width, form.Height, _<br />
me_hdc, 0, 0, win32api.SRCCOPY)<br />
bm_gr.ReleaseHdc(bm_hdc)<br />
<br />
' Return the result.<br />
Return bm<br />
End Function<br />
All calls to the win32api class are API calls or constants that can be found in the program APIViewer
Regards,
1tg46
Check out 3D Game Development with Dark Basic Professional [^]programming.
|
|
|
|
|
Hello,
I started an appl. for a touchscreen, where I designed a alphanumeric input pad.
When I now Click the Button for "a" I have to use the string.Insert Method to get the "a" in my text.
It would be much faster, if its possible to force this Event directly in the TextBox.
Thanks for your time,
Martin
|
|
|
|
|
It is impossible to fire an event on an object from code outside of that object. You might want to try calling the SendKeys.Send method.
Josh
|
|
|
|
|
First, thanks very much for your answer.
After the Buttonclick, I just had to set the .Focus() on my TextBox and then the .Send Method.
This works fine.
But is it also possible to simulate a cursor movement by using a Method like this?
Thanks again for youre time
Martin
|
|
|
|
|
You can use the Cursor.Position property to set the location of the mouse cursor.
Josh
|
|
|
|
|
Sorry,
What I meant was the "Cursor"-Key Up,Down,Left and Right.
This should influence the SelectionStart Property of my TextBox.
Thanks for your help,
Martin
|
|
|
|
|
What you are referring to is called the "caret" - the "cursor" is what the mouse moves. Outside of setting the SelectionStart property, I am not aware of any managed API which moves the caret. If SelectionStart does not do what you need, there might be some Win32 function that you can PInvoke. Check out www.pinvoke.net[^] for info about that.
Josh
-- modified at 7:58 Friday 9th June, 2006
Perhaps you can call SendKeys.Send and pass in a symbol representing one of the Arrow keys. That might move the caret, too.
|
|
|
|
|
Thanks a lot.
It was so simple just for example: SendKeys.Send("{UP}");
I think I have to parse my code now for cleaning all this dirty workarounds I made the last week.
Here I found a good list for the Symbols.
http://www.codeproject.com/cpp/sendkeys_cpp_Article.asp[^]
So again,
thanks a lot for your help and time,
Martin
|
|
|
|
|
hi
how can i auto install .net framework 2.0 and windows installer 3.0 if target system have not installed .net framework and windows installer 3.0
more :
i have a project that writed on c#.net 2005 !!!
please help me
|
|
|
|
|
Use NSIS [^]which will run on any system and you can use it as a bootstrapper for the actual installation.
Of course I'd say use NSIS from the start since you have complete control and it doesn't require anything special to run.
That's one way around it, I'm sure there are others.
You know you're a Land Rover owner when the best route from point A to point B is through the mud.
Ed
|
|
|
|
|
i add a custom action in my setup project for SQL MSDE but i don't know
the condition i write in the condition property.
the condition check if MSDE already exists don't setupo it else do
|
|
|
|
|
Hi there,
I'm looking for a .NET based sample application which is using the SkyWave
API functionality to get the tracking data (position data) from the SkyWave server.
Is there anybody here to know such this sample application?
I've asked it from SkayWave support center (support.skywave.com) but their supports are not too good.
Thank you in advance.
|
|
|
|
|
how con i check for the internet connection status changes?
every time the pc goes online/offline i want to make an action...
any ideas?
Thanks
VentoEngine corp.
Program your life ^^
|
|
|
|
|
See NetworkChange.NetworkAvailabilityChanged .
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
does it work if i have an usb modem? or a router?
VentoEngine corp.
Program your life ^^
|
|
|
|