Ускорить работу с файлом - C#
Формулировка задачи:
Программа создает sitemap.xml файл для индексации поисковиками.
Работает ну очень долго. Помогите ускорить. a = примерно 25000
public static void Main(string[] args) { using (DB_A05F1F_Frol4icContext db = new DB_A05F1F_Frol4icContext()) { IEnumerable<string> TitleIDs = new string[] { }; //Enumerable.Empty<string>(); TitleIDs = db.TitleIds.Select(x => x.TitleId);//.ToList(); IEnumerable<string> TitleIDs2 = new string[] { }; TitleIDs2 = TitleIDs2.Concat(new[] { "<?xml version = "1.0" encoding = "UTF-8" ?>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<urlset xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9">" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/Account/Login</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/account/login?ReturnUrl=%2FAccount%2FSettings%3FName%3D</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/Home/Contact</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/Account/Register</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/Account/ForgotPass</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); foreach (var a in TitleIDs) { TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=en" + "</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=de" + "</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=fr" + "</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=es" + "</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=it" + "</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<url>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=ru" + "</loc>" }); TitleIDs2 = TitleIDs2.Concat(new[] { "</url>" }); } TitleIDs2 = TitleIDs2.Concat(new[] { "</urlset>" }); var logPath = System.IO.Path.GetFullPath(@"D:\sitemap.xml"); //.GetTempFileName(); File.WriteAllLines(logPath, TitleIDs2);//.Create(logPath); } }
Решение задачи: «Ускорить работу с файлом»
textual
Листинг программы
public static void Main(string[] args) { using (var writer = new StreamWriter(File.Create(@"D:\sitemap.xml"))) using (DB_A05F1F_Frol4icContext db = new DB_A05F1F_Frol4icContext()) { writer.WriteLine("<?xml version = \"1.0\" encoding = \"UTF-8\" ?>"); writer.WriteLine("<urlset xmlns = \"http://www.sitemaps.org/schemas/sitemap/0.9\">"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/Account/Login</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/account/login?ReturnUrl=%2FAccount%2FSettings%3FName%3D</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/Home/Contact</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/Account/Register</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/Account/ForgotPass</loc>"); writer.WriteLine("</url>"); foreach (var a in db.TitleIds.Select(x => x.TitleId)) { writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=en" + "</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=de" + "</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=fr" + "</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=es" + "</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=it" + "</loc>"); writer.WriteLine("</url>"); writer.WriteLine("<url>"); writer.WriteLine("<loc>http://yttext.com/SubsView/SubsView?titleID=" + a + "&culture=ru" + "</loc>"); writer.WriteLine("</url>"); } writer.WriteLine("</urlset>"); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д