How to Convert XML to Array (5X6) then rotate -90 degrees?

55 Views Asked by At

I have an XML file which I saved in notepad:

<Layouts>
<BinCode>11111</BinCode>
<BinCode>11111</BinCode>
<BinCode>11112</BinCode>
<BinCode>11121</BinCode>
<BinCode>11111</BinCode>
<BinCode>11211</BinCode>
</Layouts>

I want to Convert this XML file to array then rotate the array to -90 Degree.

1

There are 1 best solutions below

2
On

Please note that the conversion back to XML and chosen data types are just illustrative.

I've just put an algorithm pointer for you to get you started.

Basically, what we do is we create a main array which the length of is the number of digits which is 5, each of those arrays will contain an array of 6 digits.

We're going to populate them from the back, so we'll reverse the strings from the bottom up.

XmlDocument xml = new XmlDocument();
xml.LoadXml(@"
            <Layouts>
            <BinCode>11111</BinCode>
            <BinCode>11111</BinCode>
            <BinCode>11112</BinCode>
            <BinCode>11121</BinCode>
            <BinCode>11111</BinCode>
            <BinCode>11211</BinCode>
            </Layouts>
        ");


        var binCodes = xml.DocumentElement.ChildNodes;
        int digitsInMatrix = binCodes[0].InnerText.Length;
        int[][] ints = new int[digitsInMatrix][];

        for (int d = binCodes.Count - 1; d >= 0; d--)
        {
            for (int i = digitsInMatrix - 1; i >= 0; i--)
            {
                if (ints[i] == null)
                    ints[i] = new int[binCodes.Count];

                char item = binCodes[d].InnerText[i];
                ints[i][d] = int.Parse(item.ToString());
            }
        }

        string updatedXML = string.Format("<Layouts>{0}</Layouts>", 
            string.Join("", 
                ints.Select(x => 
                    string.Format("<BinCode>{0}</BinCode>", string.Join("", x)))));