none
read xML node only from last point on file, even if application is restarted RRS feed

  • Question

  • 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[i]["ComType"].InnerText;
    
                                if (ComType == "Out")
                                {
    							
                                string chksum = topNode[i]["Checksum"].InnerText;
                                string Ticktype = topNode[i]["TicketType"].InnerText;
                                string CUType = topNode[i]["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[i]["ComType"].InnerText; if (ComType == "Out") { string chksum = topNode[i]["Checksum"].InnerText; string Ticktype = topNode[i]["TicketType"].InnerText; string CUType = topNode[i]["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"); ---> } --> } --> }


    Does this sound/look correct?

    Would this be the best way to do this? or is there a simpler, better way

    Thanks




    • Edited by G-Oker Thursday, August 8, 2019 7:08 AM
    Wednesday, August 7, 2019 6:35 PM

Answers

  • Hi,

    My solution to this was to write the node count and the files last viewed date/time stamp to node attributes in an XML, then check these to the details of the current file when next accessed.

    If the files current details are > the ones in the XML, then ask how to user want to proceed, else carry on as normal.

    Thank you, everyone, for your help on this though.

    • Marked as answer by G-Oker Tuesday, September 3, 2019 8:54 AM
    Tuesday, September 3, 2019 8:54 AM

All replies

  • Hi G-Oker,

    Thank you for posting here.

    First, I think it is best for you to provide the related xml, which will help us to reproduce your problem.

    Second, If you don't provide a complete code, please provide it.

    We are waiting for your update.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 8, 2019 7:53 AM
    Moderator
  • Hi,

    My solution to this was to write the node count and the files last viewed date/time stamp to node attributes in an XML, then check these to the details of the current file when next accessed.

    If the files current details are > the ones in the XML, then ask how to user want to proceed, else carry on as normal.

    Thank you, everyone, for your help on this though.

    • Marked as answer by G-Oker Tuesday, September 3, 2019 8:54 AM
    Tuesday, September 3, 2019 8:54 AM