Why does this work but the other one fails( Linq To Xml)

reetavainue

New Member
I tried this simple query on LinqPad and scratched by head as to why the second query works but the first one just does not alter the list(descen).
  • How do i make it work. I use this code to clone and modify xml attribute,value
Fails\[code\]var doc = XElement.Parse("<Root><Descendants>Welcome</Descendants><Descendants>Stack</Descendants><Descendants>Overflow</Descendants></Root>");var descen = (from des in doc.Descendants("Descendants") select new XElement(des));foreach (var desc in descen){ desc.Value += DateTime.UtcNow;}descen.Dump();doc.Dump();\[/code\]Works\[code\]var doc = XElement.Parse("<Root><Descendants>Welcome</Descendants><Descendants>Stack</Descendants><Descendants>Overflow</Descendants></Root>");var descen = (from des in doc.Descendants("Descendants") select new XElement(des));foreach (var desc in doc.Descendants("Descendants")){ desc.Value += DateTime.UtcNow;}descen.Dump();doc.Dump();\[/code\]Stall's my PC ?? WTH\[code\]var doc = XElement.Parse("<Root><Descendants>Welcome</Descendants><Descendants>Stack</Descendants><Descendants>Overflow</Descendants></Root>");var descen = (from des in doc.Descendants("Descendants") select des);foreach (var desc in descen){ desc.Value += DateTime.UtcNow;}var instance = from t in descen select new XElement(t);doc.Elements("Descendants").LastOrDefault().AddAfterSelf(instance);descen.Dump();\[/code\]
 
Back
Top