So recently I was dealing with an internal program that was easily confused and would create XML files with the elements in the wrong order for the schemas used in other internal programs that read these Xml files. I started looking for a fast way to reorder these elements and found this forum post which while on the right track was a bit more complicated than I really needed. So I came up with this solution.
| Bad File | Good File |
<data name="ConfigName">
<One>Item One</One>
<Three>
<A>Item A</A>
<C>Item C</C>
<C>Item C-2</C>
<B>Item B</B>
</Three>
<Two>Item Two</Two>
</data>
|
<data name="ConfigName">
<One>Item One</One>
<Two>Item Two</Two>
<Three>
<A>Item A</A>
<B>Item B</B>
<C>Item C</C>
<C>Item C-2</C>
</Three>
</data>
|
Reorder Solution
string[] rootOrder = new string[] { "One", "Two", "Three" };
string[] threeOrder = new string[] { "A", "B", "C" };
XDocument xdoc = XDocument.Load("file.xml");
Reorder(xdoc, rootOrder);
Reorder(xdoc.Root.Elements("Three"), threeOrder);
void Reorder(XDocument xdoc, params string[] order)
{
xdoc.Root.ReplaceWith(Reorder(xdoc.Root, order));
}
void Reorder(IEnumerable<XElement> els, params string[] order)
{
foreach (XElement el in els.ToArray())
el.ReplaceWith(Reorder(el, order));
}
XElement Reorder(XElement root, params string[] order)
{
return new XElement(root.Name,
from el in order
select root.Elements(el),
root.Attributes());
}