0
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.

3
  • 2
    Use LINQ and specifically LINQ to XML. Research it. You have like 30 lines of code you can just delete. Commented Aug 13, 2020 at 13:10
  • 2
    Do you have sample of xml? Commented Aug 13, 2020 at 13:11
  • 5
    Did you use bubblesort as a homework exercise? There are almost no real-world scenarios where bubblesort is the algorithm of preference for anything, and especially not since the framework has lots of sorting options built in (most notably 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). Commented Aug 13, 2020 at 13:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.