Your xml output file is incorrect, but if the input was like this:
<Phrase Entry="ID"> <Ans number="1"> <Blah LastName="Bornery" Name="John" Age="23"/> </Ans> </Phrase>
Then the following code
static string SortAttributes(string xml) { var doc = XDocument.Parse(xml); foreach (XElement element in doc.Descendants()) { var attrs = element.Attributes().ToList(); attrs.Remove(); attrs.Sort((a, b) => a.Name.LocalName.CompareTo(b.Name.LocalName)); element.Add(attrs); } xml = doc.ToString(); return xml; }
Will return it
<Phrase Entry="ID"> <Ans number="1"> <Blah Age="23" LastName="Bornery" Name="John" /> </Ans> </Phrase>
Muhammad Hasan Khan
source share