public void AddXMLItem(string item)
{
//check if duplicate//
if (IsDuplicateInXml(item))
{
RemoveXMLItem(item);//remove duplicate item. later add it so that it will be (move) up on the list
}
//no duplicate. open XML for writing. Add to list //
XmlDocument xd = new XmlDocument();
try
{
xd.Load(XMLFilename);//root element must exist in xml file
}
catch (Exception ex)
{
logService.WriteToLogLevel("Error adding XML entry in " + XMLFilename, LogLevelEnum.Error);
logService.WriteToLogLevel(ex.Message, LogLevelEnum.Error);
}
///get root element
XmlNode xn = xd.SelectSingleNode("//recent");
//if (xn == null)//if there is no <recent> tag
//{
// XmlNode rootNode = xd.CreateElement("recent");
// xd.AppendChild(rootNode);
//}
//check if MaxItems is reached. if yes then remove last item then add new
XmlNodeList xnlst = xd.GetElementsByTagName("item");
if (xnlst.Count == MaxItems)
{
xn.RemoveChild(xn.LastChild);
}
//Add new Node//
XmlNode itemnode = xd.CreateElement("item");
itemnode.InnerText = item;
xn.InsertBefore(itemnode, xn.FirstChild);//insert on top
xd.Save(XMLFilename);
//Refesh
RefreshXMLItems();
}