1) There are values ​​in txt

160 170 180 190 

2) there is xml

 <cars> <car><speed>aaa160bbb</speed></car> <car><speed>ccc170ddd</speed></car> <car><speed>eee180fff</speed></car> <car><speed>ggg190hhh</speed></car> 

Search for values ​​from the txt file in the xml file and, if found, then replace with 11111. If not, then read another value from txt

It should work

 <cars> <car><speed>aaa11111bbb</speed></car> <car><speed>ccc11111ddd</speed></car> <car><speed>eee11111fff</speed></car> <car><speed>ggg11111hhh</speed></car> </cars> 

And it would be desirable in winforms statusStrip that in the process of searching and replacing the counter is displayed (as in the screenshot: Processed elements / all elements in txt):

Counter of elements

And at the end of the replacements "display the number of speed tags found".

You can pause for a few seconds after every 5000 replacements.

  • and in <speed>ggg190hhh</speed> can be only one 190 or several? if one that see my answer. - Stack

1 answer 1

 // #r "System.Xml.Linq" // #r "System.Windows.Forms" using System.IO; using System.Xml.Linq; using System.Windows.Forms; using System.Linq; Task Process(string file, string srcXml, string trgXml, Action<int> cb) { var sc = SynchronizationContext.Current; // запомнили основной поток return Task.Run(() => { // выполняется в отдельном потоке var e = XElement.Load(srcXml); var c = 0; if (e.Descendants("speed").FirstOrDefault() == null) return; foreach (var line in File.ReadLines(file)) { sc.Send(v => cb((int) v), c++); // передаем в основной поток foreach (var s in e.Descendants("speed")) if (s.Value.Contains(line)) s.Value = s.Value.Replace(line, "11111"); } e.Save(trgXml); }); } 

 // для проверки работы Process var f = new Form(); var s = new StatusStrip() { Parent = f }; var p = new ToolStripStatusLabel(); s.Items.Add(p); f.Menu = new MainMenu(); f.Menu.MenuItems.Add(new MenuItem("Start", (s,e) => { Process( @"C:\Temp\file.txt", @"C:\Temp\cars.xml", @"c:\temp\cars.new.xml", count => p.Text = count.ToString() // выводим количество обработанных строк ).ContinueWith(t => MessageBox.Show("Completed " + t.Exception)); })); f.ShowDialog(); 
  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin