Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
At the moment iI got this:

    class robot
    {
        Configuratie config = new Configuratie();
        short[,] AlleCoordinaten = new short[3, 6]
        {
            {1,2,3,4,5,6},
            {6,5,4,3,2,1},
            {2,3,4,5,6,7}
        };
    }

But I want to put that array in a XML-file, so this is what I tried:

    class robot
    {
    Configuratie config = new Configuratie();

        short[,] AlleCoordinaten = new short[3, 6]
        {
           // errors are given here
            {(config.GetIntConfig("robot","positoin1"))},
            {(config.GetIntConfig("robot","positoin2"))},
            {(config.GetIntConfig("robot","positoin3"))}
        };
    }

configuration file:

        class Configuratie
        {
            private XDocument xdoc;

            public Configuratie()
            {
                xdoc = XDocument.Load("configuratie.xml");
            }
        public int GetIntConfig(string desc1, string desc2)
        {
            int value = 0;
            if (string.IsNullOrEmpty(desc1))
            {
                value = 0;
            }
            if (!string.IsNullOrEmpty(desc1) && !string.IsNullOrEmpty(desc2))
            {
                foreach (XElement node in xdoc.Descendants(desc1).Descendants(desc2))
                {
                    value = Convert.ToInt16(node.Value);
                }
            }
            if (!string.IsNullOrEmpty(desc1) && string.IsNullOrEmpty(desc2))
            {
                foreach (XElement node in xdoc.Descendants(desc1))
                {
                    value = Convert.ToInt16(node.Value);
                }
            }
            return value;
            }
        }


XML file:

    <robot>
    <position1>1</position1>
    <position1>2</position1>
    <position1>3</position1>
    <position1>4</position1>
    <position1>5</position1>
    <position1>6</position1>
    etc...
    <position3>7</position3>
    </robot>

It still isnt working, could you guys help me with what I did wrong and maybe give an example.
Posted
Updated 6-Feb-14 3:37am
v2
Comments
joshrduncan2012 6-Feb-14 9:15am    
Instead of "It still isn't working..." can you please tell us why it isn't working and show us what the compiler is pointing to for the error (if any)?
Member 10576431 6-Feb-14 9:18am    
Sure, i forgot to gave the errors, here there are: ''an array initializer of length 6 is expected.'' And: ''a field initializer cannot reference the nonstatic field method or property.''
BillWoodruff 6-Feb-14 9:29am    
It's also important to state where in your code the errors occurred.
Member 10576431 6-Feb-14 9:37am    
They occur here : short[,] AlleCoordinaten = new short[3, 6]

I'd suggest you focus on serializing/writing an XML file, or a JSON file, as outlined below.

Whichever format you used, de-serializing/reading the file back into an Array would be easy.

The "catch" is, as you may know, that .NET's Serialize facility does not support multi-dimensional Arrays.

You could easily use something like the FastJSON serializer/de-serializer by Mehdi Gholam here on CP, which will handle multi-dimensional Arrays: [^], and the resulting file would be fairly human-readable.

Another technique is to make your Array a jagged Array: i.e., short[][]: that can be serialized; see: [^]. If you re-structured your Array like this:
C#
private short[][] AlleCoordinaten2 = new short[3][]
{
    new short[] {1,2,3,4,5,6},
    new short[] {6,5,4,3,2,1},
    new short[] {2,3,4,5,6,7}
};
You could then write it to XMl like this:
C#
// required
using System.IO;
using System.Xml.Serialization;

private string fPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

private void WriteArrayToXML_Click(object sender, EventArgs e)
{
    using (var stream = File.Create(fPath + @"/AlleCoordinaten.xml"))
    {
        var serializer = new XmlSerializer(typeof(short[][]));
        serializer.Serialize(stream, AlleCoordinaten2);
    }
}
The resulting XML would look like this:
XML
<arrayofarrayofshort xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <arrayofshort>
    <short>1</short>
    <short>2</short>
    <short>3</short>
    <short>4</short>
    <short>5</short>
    <short>6</short>
  </arrayofshort>
  <arrayofshort>
    <short>6</short>
    <short>5</short>
    <short>4</short>
    <short>3</short>
    <short>2</short>
    <short>1</short>
  </arrayofshort>
  <arrayofshort>
    <short>2</short>
    <short>3</short>
    <short>4</short>
    <short>5</short>
    <short>6</short>
    <short>7</short>
  </arrayofshort>
</arrayofarrayofshort>
Some folks write code to convert multi-dimensional arrays to jagged arrays for serialization, and the reverse: you can find discussion of that by Googling on: "serialize jagged arrays." I'd try to avoid techniques like those, if possible.
 
Share this answer
 
v3
Comments
Pete O'Hanlon 6-Feb-14 10:26am    
Excellent answer Bill.
The error you are getting is because you are allocating an array of 3,6 short values here:
C#
short[,] AlleCoordinaten = new short[3, 6]
{
  // errors are given here
  {(config.GetIntConfig("robot","positoin1"))},
  {(config.GetIntConfig("robot","positoin2"))},
  {(config.GetIntConfig("robot","positoin3"))}
};
So, that's 3 rows of 1 element instead of 3 rows of 6 elements. As you aren't assigning the values up front, you would need to move the actual allocation of the elements. Put them in a method body and call that - so, you get:
C#
short[,] AlleCoordinaten;

private void AllocateCoords()
{
  AlleCoordinaten = new short[3,6]
  {
    { 
      GetConfig("positoin1"),
      GetConfig("positoin2"),
      GetConfig("positoin3"),
      GetConfig("positoin4"),
      GetConfig("positoin5"),
      GetConfig("positoin6"),
    },
    { 
      GetConfig("positoin7"),
      GetConfig("positoin8"),
      GetConfig("positoin9"),
      GetConfig("positoin10"),
      GetConfig("positoin11"),
      GetConfig("positoin12"),
    },
    { 
      GetConfig("positoin13"),
      GetConfig("positoin14"),
      GetConfig("positoin15"),
      GetConfig("positoin16"),
      GetConfig("positoin17"),
      GetConfig("positoin18"),
    }
  }
}

private int GetConfig(string position)
{
  return config.GetIntConfig("robot", position);
}
If I were doing this, I'd look at using a different structure and allocating the values from there, but that's just me - effectively, what you have here is a set of magic values; I'd feel more comfortable with meaningful settings.
 
Share this answer
 
Comments
Member 10576431 6-Feb-14 10:16am    
I copied it but it tells me : cannot implicitly convert type int to short
Pete O'Hanlon 6-Feb-14 10:26am    
Oops, that's right. Change the data type of the array from short to int.
Member 10576431 6-Feb-14 10:29am    
Did that, but gave another error in another methode
Member 10576431 6-Feb-14 10:34am    
class robot
{
Configuratie config = new Configuratie();
int[,] AlleCoordinaten;

private void AllocateCoords()
{
AlleCoordinaten = new int[3, 6]
{
{
GetConfig("positoin1"),
GetConfig("positoin2"),
GetConfig("positoin3"),
GetConfig("positoin4"),
GetConfig("positoin5"),
GetConfig("positoin6"),
},
{
GetConfig("positoin7"),
GetConfig("positoin8"),
GetConfig("positoin9"),
GetConfig("positoin10"),
GetConfig("positoin11"),
GetConfig("positoin12"),
},
{
GetConfig("positoin13"),
GetConfig("positoin14"),
GetConfig("positoin15"),
GetConfig("positoin16"),
GetConfig("positoin17"),
GetConfig("positoin18"),
}
};
}

private int GetConfig(string position)
{
return config.HaalIntConfig("robot", position);
}
private void methode2()
{
GoTo[0] = new Position();
for (short a=0 ; a<10 ; a++)
{
GoTo[0].degrees[0] = AlleCoordinaten[a,0];
GoTo[0].degrees[1] = AlleCoordinaten[a,1];
GoTo[0].degrees[2] = AlleCoordinaten[a,2];
GoTo[0].degrees[3] = AlleCoordinaten[a,3];
GoTo[0].degrees[4] = AlleCoordinaten[a,4];
GoTo[0].degrees[5] = AlleCoordinaten[a,5];
//here it tells me Cannot implicitly convert type int to short
}
}
}
Pete O'Hanlon 6-Feb-14 10:36am    
Whatever you store in GoTo[...].degrees[...] has to be an int as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900