从XML文件中读取一个数组 [英] Reading an array from an XML file

查看:1210
本文介绍了从XML文件中读取一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前II得到这个:

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}
    };
}

不过,我想提出一个XML的文件,数组,所以这是我的尝试:

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

class robot
{
private static XDocument xdoc = XDocument.Load("configuratie.xml");

    public Robot()
    {
        short[,] AlleCoordinaten = new short[3, 6];
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                AlleCoordinaten[i, j] = GetPositionValue("position" + (i + 1), j);
            }
        }
    }
    public static short GetPositionValue(string position,int index)
   {
       return (short)xdoc.Descendants(position).Skip(index).First();
   }
    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 The name 'AlleCoordinaten' does not exist in the currect context 
       }
    }
}

配置文件:

    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文件:

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

推荐答案

试试这个方法:

public static short GetPositionValue(string position,int index)
{
    return (short)xdoc.Descendants(position).Skip(index).First();
}

和与循环填充您的数组:

And populate your array with for loop:

下面是完整的code:

Here is the full code:

class robot
{
      private static XDocument xdoc = XDocument.Load("configuratie.xml");

      short[,] AlleCoordinaten = new short[3, 6];
      for (int i = 0; i < 3; i++)
      {
         for (int j = 0; j < 6; j++)
         {
              AlleCoordinaten[i, j] = GetPositionValue("position" + (i+1), j);
         }
      }

      public static short GetPositionValue(string position,int index)
      {
           return (short)xdoc.Descendants(position).Skip(index).First();
      }

}

请注意:更改 XDOC 定义:

private XDocument xdoc;

要:

private static XDocument xdoc;

这篇关于从XML文件中读取一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆