Exception on XML sorting

JofeWistose

New Member
I'm trying to sort elements from an XML file in LINQ by an element's attribute:\[code\]public void SortXml(){ XDocument doc = XDocument.Load(filename); XDocument datatemp = new XDocument(doc); doc.Descendants("Thing").Remove(); var module = datatemp.Descendants("Thing").OrderBy(x => (int)int.Parse(x.Attribute("ID").Value)); doc.Element("Thing").Add(module); doc.Save(filename);}\[/code\]XML:\[code\]<Entry> <Properties> <Thungs Count="2"> <Thing ID="1"> <thing1 num="8" /> <thing1 num="16" /> </Thing> <Thing ID="31"> <thing1 num="8" /> <thing1 num="16" /> </Thing> </Thungs> </Properties></Entry>\[/code\]But in the line \[code\]doc.Element("Thing").Add(module);\[/code\] I get a \[code\]NullReferenceException\[/code\].What is wrong?
 
Back
Top