I have a List that updates every minute based on a Linq query of some XML elements.
the xml changes, from time to time. It was suggested to me that I could use Hashcode to determine if any of the strings in the list have changed.
I have seen some examples of Md5 hashcode calculations for just a string, but not for a list…could someone show me a way of doing this with a list?
I tried something simple like int test = list1.GetHashCode; but the code is the same no matter what is in the list…
here is the entire method with the link query and all..note the SequenceEqual at the end:
private void GetTrackInfo()
{
_currentTitles1.Clear();
var savedxmltracks = new XDocument();
listBox1.Items.Clear();
WebClient webClient = new WebClient();
XmlDocument xmltracks = new XmlDataDocument();
try
{
xmltracks.Load(_NPUrl);
xmltracks.Save("xmltracks.xml");
}
catch (WebException ex)
{
StatusLabel1.Text = ex.Message;
}
try
{
savedxmltracks = XDocument.Load("xmltracks.xml");
}
catch (Exception ex)
{
StatusLabel1.Text = ex.Message;
}
var dateQuery = from c in savedxmltracks.Descendants("content")
select c;
_count = savedxmltracks.Element("content").Element("collection").Attribute("count").Value;
var tracksQuery1 = from c in savedxmltracks.Descendants("data")
select new
{
title = c.Attribute("title").Value,
imageurl = c.Attribute("image").Value,
price = c.Attribute("price").Value,
description = c.Attribute("productdescription").Value,
qualifier = c.Attribute("pricequalifier").Value
};
var xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("LastUsedSettings",
new XElement("TimerInterval",
new XElement("Interval", Convert.ToString(numericUpDown1.Value))),
new XElement("NowPlayingURL",
new XElement("URL", _NPUrl)),
new XElement("Email", emailAddress),
new XElement("LastUpdated", DateTime.Now.ToString())));
XElement StoreItems = new XElement("StoreItems");
int i = 0;
foreach (var c in tracksQuery1)
{
if (c.title.Length <= 40 & c.qualifier.Length <= 12 & i < 10)
{
if (c.title != null) _title1 = c.title;
if (c.imageurl != null) _imageUrl = c.imageurl;
if (c.price != null) _price = c.price;
if (c.description != null) _productDescription = c.description;
if (c.qualifier != null) _priceQualifier = c.qualifier;
//}
StoreItems.Add(new XElement("Title" + i.ToString(), _title1));
_currentTitles1.Add(_title1);
if (_oldTitles1.Count > 0)
{
Console.WriteLine("OldTitle: {0}, NewTitle: {1}", _oldTitles1[i], _currentTitles1[i]);
}
StoreItems.Add(new XElement("Price" + i.ToString(), _price));
StoreItems.Add(new XElement("Description" + i.ToString(), _productDescription));
StoreItems.Add(new XElement("PriceQualifier" + i.ToString(), _priceQualifier));
listBox1.Items.Add("Title: " + _title1);
listBox1.Items.Add("Image URL: " + _imageUrl);
listBox1.Items.Add("Price: " + _price);
listBox1.Items.Add("Description: " + _productDescription);
listBox1.Items.Add("PriceQualifier: " + _priceQualifier);
try
{
imageData = webClient.DownloadData(_imageUrl);
}
catch (WebException ex)
{
StatusLabel1.Text = ex.Message;
}
MemoryStream stream = new MemoryStream(imageData);
Image img = Image.FromStream(stream);
//Image saveimage = img;
//saveimage.Save("pic.jpg");
img.Save("pic" + i.ToString() + ".jpg");
stream.Close();
i++;
}
}
//Console.WriteLine("Count: " + _count);
Console.WriteLine("oldTitles Count: " + _oldTitles1.Count.ToString());
Console.WriteLine("currentTitles Count: " + _currentTitles1.Count.ToString());
if (_oldTitles1.Count == 0) _oldTitles1 = _currentTitles1;
if (!_oldTitles1.SequenceEqual(_currentTitles1))
{
Console.WriteLine("Items Changed!");
SendMail();
_oldTitles1 = _currentTitles1;
}
xml.Root.Add(StoreItems);
xml.Save("settings.xml");
}
I don’t think you need to bother yourself about all the hash code discussion if you are not going to have hundreds thousands of elements or if you are not going to request this function thousands times a second.
Here is a small program that will show you how much time it will take to compare 10000 element using your correct way of doing this.
At my PC it took 0.001 seconds.