G
G-Oker
Guest
Hi,
I am stuck on some logic, and therefore do not know how to deal with the issue I have, so am after some help/ guidance.
I have a program which looks to see if a specific XML file has changed and only deals with the changes(new Nodes).
So, when the program is first run, it deals with all the nodes file and adds that count to a variable (initially set to zero when the program starts), then, while it's running, if it senses a change, it reads the XML again, but from the last count. Once it has dealt with this new node(s) and amends the count with the new value (so it's only dealing with the additions to the file, which are always appended to the structure)
int nodeCount = 0;
using (myStream)
{
xmldoc.Load(FldPath + "\\filename.xml");
XmlNodeList topNode = xmldoc.GetElementsByTagName("topNode"); // Locate nodes in dropped calls file
int topNodeFound = topNode.Count; // if the above has been found, count how many times
if (topNodeFound > 0)
{
tbOutput.Focus();
for (int i = nodeCount; i < topNode.Count; i++)
{
string ComType = topNode["ComType"].InnerText;
if (ComType == "Out")
{
string chksum = topNode["Checksum"].InnerText;
string Ticktype = topNode["TicketType"].InnerText;
string CUType = topNode["Type"].InnerText;
etc...
}
else
{
tbOutput.AppendText("ComType was " + ComType + Environment.NewLine);
}
}
tbOutput.AppendText("-- EoF --");
nodeCount = topNodeFound;
}
}
The scenario I am trying to figure out and overcome is if the program is closed down and then restarted.
I don't want the program to go thought all the (previously dealt with) data again, but carry on from that last count (which is now reset to 0).
I have tried storing the files "modified Date time" to an external file and reference that, but the program only reacts to a file change, so these to values will not match.
I could store the Node count in an external file and pull that data on program startup, however, at midnight, the file is renamed, and a new file created in its place (so the node count @ midnight needs to be reset).
So, as I see it (and please correct me if I am wrong, or there is a better way), is that each time my program detects the file has changed, write the "modified" date time and Nodecount to another file, and check the last read date = today)
PSEUDO CODE
int nodeCount = 0;
using (myStream)
{
xmldoc.Load(FldPath + "\\filename.xml");
XmlNodeList topNode = xmldoc.GetElementsByTagName("topNode"); // Locate nodes in dropped calls file
int topNodeFound = topNode.Count; // if the above has been found, count how many times
---> FileInfo fileInfo = new FileInfo(FldPath + "\\filename.xml");
---> lastmodified = fileInfo.LastWriteTime.ToString().TRIM OFF TIME;
---> LastReadtime = fileInfo.LastWriteTime.ToString().TRIM OFF DATE;
---> XmlDocument xDoc - new XmlDocument();
---> xDoc.load("externalDatafile");
---> XmlNodeList nodesLastCounted = xDoc.GetElementsByTagName("currentNodeCount");
---> string nodesAlreadyCounted = nodesLastCounted[0]["currentNodeCount"].InnerText;
---> XmlNodeList LastReadDate = xDoc.GetElementsByTagName("LastReadDate");
---> string LastReadDateCounted = nodesLastCounted[0]["LastReadDate"].InnerText;
---> XmlNodeList LastReadtime = xDoc.GetElementsByTagName("LastReadtime");
---> string LastReadtimeCounted = nodesLastCounted[0]["LastReadtime"].InnerText;
--->
--->
---> if ((LastReadDateCounted = todaysDate) && (LastReadtime =< currentTime)) //CHECK IF DAY HAS ROLLED OVER
---> {
---> if (nodesAlreadyCounted>0)
---> {
---> nodeCount = nodesAlreadyCounted;
---> }
---> else
---> {
---> nodeCount =0;
---> }
---> else
---> {
---> nodeCount =0;
---> }
if (topNodeFound > 0)
{
tbOutput.Focus();
for (int i = nodeCount; i < topNode.Count; i++)
{
string ComType = topNode["ComType"].InnerText;
if (ComType == "Out")
{
string chksum = topNode["Checksum"].InnerText;
string Ticktype = topNode["TicketType"].InnerText;
string CUType = topNode["Type"].InnerText;
etc...
}
else
{
tbOutput.AppendText("ComType was " + ComType + Environment.NewLine);
}
}
---> //nodeCount = topNodeFound;
---> XmlDocument ydoc = new XmlDocument();
---> ydoc.Load("externalDatafile");
---> XmlNodeList lastDatE = ydoc.GetElementsByTagName("LastReadDate"); // Locate nodes in configuration file
---> int LastDateFound = lastXMLDatE.Count;
--->
---> if (LastDateFound == 0)
---> {
---> XDocument ydoc = XDocument.Load("externalDatafile");
---> XElement Settings = ydoc.Element("Settings");
---> Settings.Add(new XElement("LastReadDate", lastmodified));
---> Settings.Add(new XElement("currentNodeCount", nodeCount));
---> Settings.Add(new XElement("LastReadtime", lastModifiedTime));
---> ydoc.Save("externalDatafile");
---> }
---> else
---> {
---> XmlNodeList nodeList = ydoc.GetElementsByTagName("LastReadDate");
---> nodeList[0].InnerXml = lastmodified;
---> XmlNodeList nodeList2 = ydoc.GetElementsByTagName("currentNodeCount");
---> nodeList2[0].InnerXml = nodeCount);
---> XmlNodeList nodeList3 = ydoc.GetElementsByTagName("LastReadtime");
---> nodeList3[0].InnerXml = LastReadtime);
---> ydoc.Save("externalDatafile");
---> }
--> }
--> }
Do this sound/look <g class="gr_ gr_293 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="293" id="293">correct ?</g> Would this be the best way to do this? or is there a similar, better way
Thanks
Continue reading...
I am stuck on some logic, and therefore do not know how to deal with the issue I have, so am after some help/ guidance.
I have a program which looks to see if a specific XML file has changed and only deals with the changes(new Nodes).
So, when the program is first run, it deals with all the nodes file and adds that count to a variable (initially set to zero when the program starts), then, while it's running, if it senses a change, it reads the XML again, but from the last count. Once it has dealt with this new node(s) and amends the count with the new value (so it's only dealing with the additions to the file, which are always appended to the structure)
int nodeCount = 0;
using (myStream)
{
xmldoc.Load(FldPath + "\\filename.xml");
XmlNodeList topNode = xmldoc.GetElementsByTagName("topNode"); // Locate nodes in dropped calls file
int topNodeFound = topNode.Count; // if the above has been found, count how many times
if (topNodeFound > 0)
{
tbOutput.Focus();
for (int i = nodeCount; i < topNode.Count; i++)
{
string ComType = topNode["ComType"].InnerText;
if (ComType == "Out")
{
string chksum = topNode["Checksum"].InnerText;
string Ticktype = topNode["TicketType"].InnerText;
string CUType = topNode["Type"].InnerText;
etc...
}
else
{
tbOutput.AppendText("ComType was " + ComType + Environment.NewLine);
}
}
tbOutput.AppendText("-- EoF --");
nodeCount = topNodeFound;
}
}
The scenario I am trying to figure out and overcome is if the program is closed down and then restarted.
I don't want the program to go thought all the (previously dealt with) data again, but carry on from that last count (which is now reset to 0).
I have tried storing the files "modified Date time" to an external file and reference that, but the program only reacts to a file change, so these to values will not match.
I could store the Node count in an external file and pull that data on program startup, however, at midnight, the file is renamed, and a new file created in its place (so the node count @ midnight needs to be reset).
So, as I see it (and please correct me if I am wrong, or there is a better way), is that each time my program detects the file has changed, write the "modified" date time and Nodecount to another file, and check the last read date = today)
PSEUDO CODE
int nodeCount = 0;
using (myStream)
{
xmldoc.Load(FldPath + "\\filename.xml");
XmlNodeList topNode = xmldoc.GetElementsByTagName("topNode"); // Locate nodes in dropped calls file
int topNodeFound = topNode.Count; // if the above has been found, count how many times
---> FileInfo fileInfo = new FileInfo(FldPath + "\\filename.xml");
---> lastmodified = fileInfo.LastWriteTime.ToString().TRIM OFF TIME;
---> LastReadtime = fileInfo.LastWriteTime.ToString().TRIM OFF DATE;
---> XmlDocument xDoc - new XmlDocument();
---> xDoc.load("externalDatafile");
---> XmlNodeList nodesLastCounted = xDoc.GetElementsByTagName("currentNodeCount");
---> string nodesAlreadyCounted = nodesLastCounted[0]["currentNodeCount"].InnerText;
---> XmlNodeList LastReadDate = xDoc.GetElementsByTagName("LastReadDate");
---> string LastReadDateCounted = nodesLastCounted[0]["LastReadDate"].InnerText;
---> XmlNodeList LastReadtime = xDoc.GetElementsByTagName("LastReadtime");
---> string LastReadtimeCounted = nodesLastCounted[0]["LastReadtime"].InnerText;
--->
--->
---> if ((LastReadDateCounted = todaysDate) && (LastReadtime =< currentTime)) //CHECK IF DAY HAS ROLLED OVER
---> {
---> if (nodesAlreadyCounted>0)
---> {
---> nodeCount = nodesAlreadyCounted;
---> }
---> else
---> {
---> nodeCount =0;
---> }
---> else
---> {
---> nodeCount =0;
---> }
if (topNodeFound > 0)
{
tbOutput.Focus();
for (int i = nodeCount; i < topNode.Count; i++)
{
string ComType = topNode["ComType"].InnerText;
if (ComType == "Out")
{
string chksum = topNode["Checksum"].InnerText;
string Ticktype = topNode["TicketType"].InnerText;
string CUType = topNode["Type"].InnerText;
etc...
}
else
{
tbOutput.AppendText("ComType was " + ComType + Environment.NewLine);
}
}
---> //nodeCount = topNodeFound;
---> XmlDocument ydoc = new XmlDocument();
---> ydoc.Load("externalDatafile");
---> XmlNodeList lastDatE = ydoc.GetElementsByTagName("LastReadDate"); // Locate nodes in configuration file
---> int LastDateFound = lastXMLDatE.Count;
--->
---> if (LastDateFound == 0)
---> {
---> XDocument ydoc = XDocument.Load("externalDatafile");
---> XElement Settings = ydoc.Element("Settings");
---> Settings.Add(new XElement("LastReadDate", lastmodified));
---> Settings.Add(new XElement("currentNodeCount", nodeCount));
---> Settings.Add(new XElement("LastReadtime", lastModifiedTime));
---> ydoc.Save("externalDatafile");
---> }
---> else
---> {
---> XmlNodeList nodeList = ydoc.GetElementsByTagName("LastReadDate");
---> nodeList[0].InnerXml = lastmodified;
---> XmlNodeList nodeList2 = ydoc.GetElementsByTagName("currentNodeCount");
---> nodeList2[0].InnerXml = nodeCount);
---> XmlNodeList nodeList3 = ydoc.GetElementsByTagName("LastReadtime");
---> nodeList3[0].InnerXml = LastReadtime);
---> ydoc.Save("externalDatafile");
---> }
--> }
--> }
Do this sound/look <g class="gr_ gr_293 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="293" id="293">correct ?</g> Would this be the best way to do this? or is there a similar, better way
Thanks
Continue reading...