for (int i = 0; i < node1.ChildNodes.Count; i++)
{
if (i == 0)
{
XmlNode node2 = node1.FirstChild;
XmlNode node3 = node2;
XmlNode node11 = node2.CloneNode(true);
string b = node3.NextSibling.Attributes["name"].Value.ToString();
while (node2.Attributes["name"].Value.ToString().CompareTo(b) < 0)
{
XmlNode node4 = node3.NextSibling;
node1.RemoveChild(node3);
doc.DocumentElement.InsertAfter(node11, node4);
node3 = node4.NextSibling;
if (node3 != node1.LastChild)
{
b = node3.NextSibling.Attributes["name"].Value.ToString();
}
else
{
b = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
}
}
}
if (i > 0)
{
XmlNode node2 = node1.FirstChild;
for (int k = 0; k < i; k++)
{
if (node2 != node1.LastChild)
{
node2 = node2.NextSibling;
}
}
string a = "0";
if (node2 != node1.LastChild)
{
a = node2.NextSibling.Attributes["name"].Value.ToString();
}
int z1 = 0;
XmlNode node3 = node2;
XmlNode node10 = node2.CloneNode(true);
while (node2.Attributes["name"].Value.ToString().CompareTo(a) < 0)
{
XmlNode node4 = node3.NextSibling;
node1.RemoveChild(node3);
doc.DocumentElement.InsertAfter(node10, node4);
if (node3 != node1.LastChild)
{
node3 = node4.NextSibling;
}
else
{
node3 = node1.FirstChild;
}
if (node3 != node1.LastChild)
{
a = node3.NextSibling.Attributes["name"].Value.ToString();
}
else
{
a = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
}
z1+=1;
}
}
When I am running this code, my XML-file is destroyed and the most lines are deleted. My purpose is to sort the values of the attributes alphabetically. I tried to use the Bubblesort algorithm here. The principe is that one node is fixed and this is checked with his next siblings and it changes the position with this sibling and this loop will stop when the next sibling is after him in alphabetical order. The principe of the change is that the fixed node will be deleted and after this next sibling inserted but it isn't working.
Enumerable.OrderBy). Even if you really wanted to manipulate XML elements in-place for some reason, insertion sort would be better (and is better to have memorized for such occasions than bubblesort).