Friday, 18 July 2014

Writing data to xml without for loop



I am working on NopCommerce 2.40, and trying to generate sitemap using c# to submit it to google webmaster tools.


My code



_writer = new XmlTextWriter(stream, Encoding.UTF8);
_writer.Formatting = Formatting.Indented;
_writer.WriteStartDocument();
_writer.WriteStartElement("urlset");
_writer.WriteAttributeString("xmlns", "http://ift.tt/xwbjRF");
_writer.WriteAttributeString("xmlns:xsi", "http://ift.tt/ra1lAU");
_writer.WriteAttributeString("xsi:schemaLocation", "http://ift.tt/xwbjRF http://ift.tt/Av6BHN");

GenerateUrlNodes();

_writer.WriteEndElement();
_writer.Close();


In this GenerateUrlNodes() method different methods are called e.g



WriteStores();
WriteStoresCategories();
WriteStoresProducts();


In third method of writing store products.



foreach (var store in stores)
{

//TODO add a method for getting URL (use routing because it handles all SEO friendly URLs)
if (store.Id != 1)
{
var products = _productService.SearchProducts(0, 0, null, null, null, 0, null, false, 1, new List<int>(), ProductSortingEnum.Position, 0, int.MaxValue, store.Id, false);
foreach (var product in products)
{
var url = string.Format("{0}s/{1}/{2}/p/{3}/{4}", _webHelper.GetStoreLocation(false), store.Id, store.GetSeName(), product.Id, product.GetSeName());
var updateFrequency = UpdateFrequency.Weekly;
var updateTime = product.UpdatedOnUtc;
WriteUrlLocation(url, updateFrequency, updateTime);


}

}
}


Its taking too time as there are Multiple stores with 5 to 6k of products in each store. So when I try to submit this sitemap to google its throwing error of timeout. Is there any way to optimize this or avoiding for loop to generate xml


Can anybody help me with this?


No comments:

Post a Comment