C# Linq-XML find match and replace elements <p>I am writing a quick tool to convert from text to XML.</p> <p>I've created a Dictionary that contains the note number and the new name.</p> <p>I would like to search in the XML for the "INote" value (in the example is "118") and find the corresponding key in the Dictionary and replace the "Name" value (in the example is "Sound 119")</p> <p>The XML structure looks like this </p> <pre><code> &lt;list name="Quantize" type="list"&gt; &lt;item&gt; &lt;int name="Grid" value="4"/&gt; &lt;int name="Type" value="0"/&gt; &lt;float name="Swing" value="0"/&gt; &lt;int name="Unquantized" value="0"/&gt; &lt;int name="Legato" value="50"/&gt; &lt;/item&gt; &lt;/list&gt; &lt;list name="Map" type="list"&gt; &lt;item&gt; &lt;int name="INote" value="118"/&gt; &lt;int name="ONote" value="118"/&gt; &lt;int name="Channel" value="9"/&gt; &lt;float name="Length" value="200"/&gt; &lt;int name="Mute" value="0"/&gt; &lt;int name="DisplayNote" value="118"/&gt; &lt;int name="HeadSymbol" value="0"/&gt; &lt;int name="Voice" value="0"/&gt; &lt;int name="PortIndex" value="0"/&gt; &lt;string name="Name" value="Sound 119" wide="true"/&gt; &lt;int name="QuantizeIndex" value="0"/&gt; &lt;/item&gt; &lt;item&gt; .... &lt;/item&gt; &lt;/list&gt; &lt;list name="Order" type="int"&gt; &lt;item value="0"/&gt; &lt;item value="1"/&gt; &lt;item value="2"/&gt; &lt;/list&gt; </code></pre> <p>And this is my code so far: </p> <pre><code> XDocument outFile = XDocument.Load(outputFile); var currItem = from item in outFile.Descendants("item") select item; foreach (var i in currItem) // this loop is executed many times { var node = i.Element("int"); if (node ==null) continue; var name = node.Attribute("name").Value; var value = i.Element("int").Attribute("value").Value; if (name == "INote") { var str = i.Element("string").Attribute("name").Value; if (str == "Name") { var snd = i.Element("string").Attribute("value").Value; MessageBox.Show(string.Format("{0} - {1} - {2} - {3}", name, value,str,snd)); } } } </code></pre> <p>I have 2 problems : </p> <ul> <li><p>The query : is there a way to perform the checks directly in the query itself instead than in the foreach loop? (as additional info not all the elements contain the element with the "INote" attribute)</p></li> <li><p>The foreach loop : the entire loop is repeated more n time so I get the Messagebox from 0 to n multiple times.</p></li> </ul> <p>Any help would be appreciated.</p> <p><strong>ANSWER :</strong> Thanks everybody, I think I've fixed the problem after some tests :</p> <pre><code> XDocument outFile = XDocument.Load(outputFile); var NamesData = outFile.Root.Elements("list") .Where(x =&gt; x.Attribute("name").Value == "Map") .Elements("item") .Select(y =&gt; new KeyValuePair&lt;XAttribute,XAttribute&gt; ( y.Elements("int").Where(c =&gt; c.Attribute("name").Value == "INote").Select(c =&gt; c.Attribute("value")).FirstOrDefault(), y.Elements("string").Where(c =&gt; c.Attribute("name").Value == "Name").Select(c =&gt; c.Attribute("value")).FirstOrDefault() ) ); </code></pre> <p>I create a KeyValuePair of XAttributes so i can save them back easily :</p> <p>this is the rest of the method btw (Notedefinition it's just a data container)</p> <pre><code> foreach (var data in NamesData) { NoteDefinition newName; if (internalDictionary.TryGetValue(Convert.ToInt32(data.Key.Value), out newName)) { data.Value.SetValue(newName.voiceName); } } outFile.Save(outputFile); </code></pre>