C# form refreshing and XML editing.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-family:Verdana; font-size:9pt Hi,
<span style="font-family:Verdana; font-size:9pt I am trying to write a "Simple" windows form application with C# that will take an input (A date) from an XML file, then compare the date from the XML file with the current system date and then give an
output in the form of a lable with the number of days in between the input date and the current date.

The issues I am hitting are to do with resetting the date within the XML file to today’s date (So the counter starts over) and getting the form to refresh every night so that it shows the true number of days since the date in the XML file (i.e so the form
counts up 1 at midnight every night).
Just a note: I’m a complete novice when it comes to C#, I first saw it about a week ago when I was asked to write this -_-.
Thanks in advance!

Here’s what I have so far:
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <span style="font-size:small
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.Collections.Generic;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.ComponentModel;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.Data;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.Drawing;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.Linq;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.Text;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.Windows.Forms;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.Xml;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.IO;
<span style="font-family:Consolas; color:blue; font-size:9.5pt using<span style="font-family:Consolas; font-size:9.5pt System.Xml.Linq;

<span style="font-family:Consolas; color:blue; font-size:9.5pt namespace<span style="font-family:Consolas; font-size:9.5pt WindowsFormsApplication1
<span style="font-family:Consolas; font-size:9.5pt {
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:blue partial <span style="color:blue
class <span style="color:#2b91af Form1 : <span style="color:#2b91af
Form
<span style="font-family:Consolas; font-size:9.5pt <span>
{
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public Form1()
<span style="font-family:Consolas; font-size:9.5pt <span>
{
<span style="font-family:Consolas; font-size:9.5pt <span>
InitializeComponent();
<span style="font-family:Consolas; font-size:9.5pt <span>
dateGet();
<span style="font-family:Consolas; font-size:9.5pt <span>
}

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue private <span style="color:blue void button1_Click(<span style="color:blue object sender,
<span style="color:#2b91af EventArgs e)
<span style="font-family:Consolas; font-size:9.5pt <span>
{
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af DateTime newDate = <span style="color:#2b91af
DateTime.Now;
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue string newString = newDate.ToString(<span style="color:#a31515 "dd-MM-yyyy");
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af XmlDocument xmlDoc = <span style="color:blue
new <span style="color:#2b91af XmlDocument();
<span style="font-family:Consolas; font-size:9.5pt <span>
xmlDoc.Load(<span style="color:#a31515 "Outage.xml");
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af XmlNode node = xmlDoc.SelectSingleNode(<span style="color:#a31515 "root/outage/");
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green //XmlNodeList nodes = xmlDoc.SelectSingleNode("/Root/outage");
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af XmlAttribute Attributes = xmlDoc.Attributes[<span style="color:#a31515 "date"];
<span style="font-family:Consolas; font-size:9.5pt <span>
xmlDoc.Attributes[0].Value = newString; xmlDoc.Save(<span style="color:#a31515 "Outage.xml");
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue this.Refresh();
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="font-family:Consolas; font-size:9.5pt <span>
}

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue public <span style="color:#2b91af String ReadXML()<span>
{
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af XDocument xmlDocument = <span style="color:#2b91af
XDocument.Load(<span style="color:#2b91af String.Format(<span style="color:#a31515 @"{0}{1}",System.<span style="color:#2b91af Environment.CurrentDirectory,<span style="color:#a31515 "Outage.xml"));

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue var storedDate = (<span style="color:blue from setting
<span style="color:blue in xmlDocument.Element(<span style="color:#a31515 "root").Elements(<span style="color:#a31515 "outage")
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue let result = setting.Attribute(<span style="color:#a31515 "date").Value
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue select result).ToList();

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue return storedDate.First();
<span style="font-family:Consolas; font-size:9.5pt <span>
}

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue private <span style="color:blue void dateGet()
<span style="font-family:Consolas; font-size:9.5pt <span>
{
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af String dateString = ReadXML();
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af DateTime oldDate;
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af DateTime.TryParseExact(dateString, <span style="color:#a31515
"dd-MM-yyyy", System.Globalization.<span style="color:#2b91af CultureInfo.InvariantCulture, System.Globalization.<span style="color:#2b91af DateTimeStyles.None,
<span style="color:blue out oldDate);
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af DateTime newDate = <span style="color:#2b91af
DateTime.Now;
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af TimeSpan ts = newDate - oldDate;
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue int differenceInDays = ts.Days;

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue string xdiff = <span style="color:#2b91af
Convert.ToString(differenceInDays);
<span style="font-family:Consolas; font-size:9.5pt <span>
output1.Text = xdiff;
<span style="font-family:Consolas; font-size:9.5pt <span>
}

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue private <span style="color:blue void button4_Click(<span style="color:blue object sender,
<span style="color:#2b91af EventArgs e)
<span style="font-family:Consolas; font-size:9.5pt <span>
{
<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af Application.Exit();
<span style="font-family:Consolas; font-size:9.5pt <span>
}

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue private <span style="color:blue void Form1_Load(<span style="color:blue object sender,
<span style="color:#2b91af EventArgs e)
<span style="font-family:Consolas; font-size:9.5pt <span>
{

<span style="font-family:Consolas; font-size:9.5pt <span>
}

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue private <span style="color:blue void button1_Click_1(<span style="color:blue object sender,
<span style="color:#2b91af EventArgs e)
<span style="font-family:Consolas; font-size:9.5pt <span>
{

<span style="font-family:Consolas; font-size:9.5pt <span>
}

<span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue private <span style="color:blue void timer1_Tick(<span style="color:blue object sender,
<span style="color:#2b91af EventArgs e)
<span style="font-family:Consolas; font-size:9.5pt <span>
{
<span style="font-family:Consolas; font-size:9.5pt <span>
dateGet();
<span style="font-family:Consolas; font-size:9.5pt <span>
}
<span style="font-family:Consolas; font-size:9.5pt <span>
}
<span style="font-family:Consolas; font-size:9.5pt }


View the full article
 
Back
Top