using System; using System.IO; using System.Net; using System.Reflection; using System.Xml.Linq; using System.Collections.Generic; namespace Stat_Update.Classes { //Класс пораметров public class UnParam { public string path { get; set; } public string url { get; set; } public string lastver { get; set; } public string size { get; set; } public string needRestart { get; set; } } //Класс процедуры public partial class UnUpdate { #region - Объявляем переменные public UnParam UnParam { get; set; } #endregion //Запуск модуля проверки обновления public static IList<List<UnParam>> UpdateXml(string docLocale, string docServer) { List<List<UnParam>> par3 = new List<List<UnParam>>(); using (WebClient wc = new WebClient()) { List<UnParam> parLocal = new List<UnParam>(); List<UnParam> parGlobal = new List<UnParam>(); try { //читаем данные из файла XDocument docLoc = XDocument.Load(docLocale); //проходим по каждому элементу в найшей library foreach (XElement el in docLoc.Root.Elements()) { UnParam unParam = new UnParam { path = el.Attribute("path").Value, url = el.Attribute("url").Value, lastver = el.Attribute("lastver").Value, size = el.Attribute("size").Value, needRestart = el.Attribute("needRestart").Value }; parLocal.Add(unParam); } par3.Add(parLocal); XDocument docSer = XDocument.Load(docServer); foreach (XElement el in docSer.Root.Elements()) { UnParam unParam = new UnParam { path = el.Attribute("path").Value, url = el.Attribute("url").Value, lastver = el.Attribute("lastver").Value, size = el.Attribute("size").Value, needRestart = el.Attribute("needRestart").Value }; parGlobal.Add(unParam); } par3.Add(parGlobal); } catch { } } return par3; } //Запуск модуля скачивания обновления public static void Execute(IList<UnParam> unParamLocal, IList<UnParam> unParamServer) { } //Запуск обновления static void Update(UnParam requestParameters) { } } } *********Сам проект в нём объявляем******* public static IList<UnParam> Local = new List<UnParam>(); public static IList<UnParam> Global = new List<UnParam>(); *********Код допустим кнопки********* //Это фиксированная ссылка на локальный файл обновления string LocalIn = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile) + Properties.Settings.Default.LoadIn; //Это фиксированная ссылка на глобальный файл обновления string GlobalOut = Properties.Settings.Default.LoadOut; //Парсим файлы Global = UnUpdate.UpdateXml(LocalIn, GlobalOut); //UnUpdate.Execute(LocalIn, GlobalOut); 

THAT IS OUTCOMING FROM THE CODE:
Now both xml files are parsed in one sitting, and transferred back to the nested "List", but it seems to me that such a construction is cumbersome. Although what I wanted was achieved In one List there are 2 nested ones first is local xml and the second is remote xml . that's what I wanted.
Now about the optimization! Waiting for options ?!

    2 answers 2

    • In your method, you do the same thing twice . Take out the general code and do something like this:

       public static IEnumerable<UnParam> ParseXmlContents(string uri) { XDocument document = XDocument.Load(uri); foreach (XElement element in document.Root.Elements) { yield return new UnParam { path = element.Attribute("path").Value, url = element.Attribute("url").Value, lastver = element.Attribute("lastver").Value, size = element.Attribute("size").Value, needRestart = element.Attribute("needRestart").Value }; } } // Вызывающая сторона: IEnumerable<UnParam> localXmlContents = ParseXmlContents(localIn); IEnumerable<UnParam> globalXmlContents = ParseXmlContents(globalIn); 
    • You can also enter a factory method with the semantics 'UnParam FromXmlElement(XElement element);' and use it to create UnParam. objects UnParam.

    • Thank you, it didn’t occur at once to take it out separately. Good decision. - Fynjn
    • 3
      I would rewrite foreach + yield return as Select - VladD
    • @VladD Yes, it is possible, unless it is completely radically different from the original version of the author. - Costantino Rupert
    • @ Kitty: you need to teach people to the elements of FP :) - VladD
    • @VladD Something I am not sure that for the case of par3.Add(parGlobal) and catch {} knowing a piece of ФП in the form of LINQ greatly improve the situation :) - Costantino Rupert

    Remove public static List<UnParam> par = new List<UnParam>(); from the UnUpdate class. Instead of this static field (which you overwrite each time you call UpdateXml), use a local variable in the UpdateXml method.

    • Now both xml files are parsed in one sitting, and transferred back to the nested "List", but it seems to me that such a construction is cumbersome. Although what I wanted was achieved In one List there are 2 nested ones first is local xml and the second is remote xml. that's what I wanted. Now about the optimization! Waiting for options ?! - Fynjn