public void Test() { SimpleItemInfos infos = new SimpleItemInfos(); infos.Items = new ItemInfoList(); infos.Items.Add(new ItemInfo() { Selected = true, SubItems = new[] { "Item1", "SubItem1" }.ToList() }); infos.Items.Add(new ItemInfo() { Selected = false, SubItems = new[] { "Item2", "SubItem2" }.ToList() }); XElement root = new XElement("Root"); OptionFileItemInfosAdaptor adaptor = new OptionFileItemInfosAdaptor(infos, "TEST"); adaptor.SaveToXml(root); SimpleItemInfos readInfos = new SimpleItemInfos(); OptionFileItemInfosAdaptor readAdaptor = new OptionFileItemInfosAdaptor(readInfos, "TEST"); readAdaptor.LoadFromXml(root); Assert.AreEqual(readInfos.Items.Count, infos.Items.Count); for (int i = 0; i < readInfos.Items.Count; i++) { Assert.AreEqual(readInfos.Items[i].Selected, infos.Items[i].Selected); Assert.AreEqual(readInfos.Items[i].SubItems, infos.Items[i].SubItems); } }
public void TestReadFromNewVersion() { SimpleItemInfos infos = new SimpleItemInfos(); OptionFileItemInfosAdaptor adaptor = new OptionFileItemInfosAdaptor(infos, "Items"); XElement option = XElement.Load(@"../../../data/ListFileFormatNew.lst"); adaptor.LoadFromXml(option); Assert.AreEqual(2, infos.Items.Count); }
public void TestReadFromOldVersion() { SimpleItemInfos infos = new SimpleItemInfos(); var adaptor = new OptionFileItemInfosAdaptor(infos, "DatFiles"); var option = XElement.Load(@"../../../data/ListFileFormatOld.lst", LoadOptions.SetBaseUri); adaptor.LoadFromXml(option); Assert.AreEqual(1, infos.Items.Count); }
public List <string> ReadFromFile(string fileName) { if (!File.Exists(fileName)) { throw new FileNotFoundException(MyConvert.Format("List file {0} not exists", fileName)); } XElement root = XElement.Load(fileName); XElement keyNode = root.Elements().FirstOrDefault(); List <string> result; string xname = keyNode.Name.LocalName; if (xname.Equals(OldKey)) { var countElement = (from add in keyNode.Descendants() where add.Attribute("key").Value.EndsWith("_count") select add).First(); var count = Convert.ToInt32(countElement.Attribute("value").Value); var key_count = countElement.Attribute("key").Value; var key = key_count.Substring(0, key_count.Length - 6); Regex keyRegex = new Regex(key + @"_\d+$"); result = (from add in keyNode.Descendants() let curkey = add.Attribute("key").Value where keyRegex.Match(curkey).Success select add.Attribute("value").Value).ToList(); } else { SimpleItemInfos infos = new SimpleItemInfos(); OptionFileItemInfosAdaptor adaptor = new OptionFileItemInfosAdaptor(infos, xname); adaptor.LoadFromXml(XElement.Load(fileName)); result = infos.Items.GetAllItems().ToList(); } return(result); }
private void btnLoad_Click(object sender, EventArgs e) { if (this.openDialog == null) { this.openDialog = new OpenFileDialog(); this.openDialog.Filter = "File list(*.lst)|*.lst|All Files(*.*)|*.*"; } if (this.openDialog.ShowDialog(this) == DialogResult.OK) { ListFileReader reader = new ListFileReader(); List <string> dirOrFiles = reader.ReadFromFile(openDialog.FileName); if (dirOrFiles.Count == 0) { SimpleItemInfos infos = new SimpleItemInfos(); ItemInfosEventHandlers handlers = new ItemInfosEventHandlers(infos); handlers.Adaptor.LoadFromXml(XElement.Load(openDialog.FileName, LoadOptions.SetBaseUri)); dirOrFiles = infos.Items.GetAllItems().ToList(); } List <string> files = new List <string>(); foreach (string dirOrFile in dirOrFiles) { if (Directory.Exists(dirOrFile)) { files.AddRange(new DirectoryInfo(dirOrFile).GetFiles("*.dtas").ToList().ConvertAll(m => m.FullName)); continue; } files.Add(dirOrFile); } lbDtaFiles.FileNames = files.ToArray(); } }