【问题标题】:Reading an array from an XML file从 XML 文件中读取数组
【发布时间】:2014-02-04 12:24:22
【问题描述】:

目前我得到了这个:

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 文件中,所以这是我尝试的:

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>

它仍然不起作用,你们能帮我解决我做错了什么吗?也许可以举个例子。

【问题讨论】:

  • 您收到什么错误信息?我假设positoin 错字就在这里,而不是在原版中。如果没有,那么这是一个很好的起点。 ;-)
  • 我得到的错误是:预期长度为 6 的数组初始化程序。还有:字段初始值设定项不能引用非静态字段方法或属性。
  • 可能你希望 GetIntConfig 方法返回一个 int[]
  • 一切正常,唯一的问题是我收到错误“字段初始化程序无法引用非静态字段方法或属性”和“预计长度为 6 的数组初始化程序”。我还是没弄明白

标签: c# xml arrays


【解决方案1】:

使用XmlSerializer 会大大简化事情:

  • 自动调整数组大小;
  • 序列化/反序列化只是几行代码。

这样

public class Coordinate
{
    public int X1 {get; set;}
    public int Y1 {get; set;}
    public int Z1 {get; set;}
    public int X2 {get; set;}
    public int Y2 {get; set;}
    public int Z2 {get; set;}

    public Coordinate(int x1, int y1, int z1, int x2, int y2, int z2)
    {
        X1 = x1; Y1 = y1; Z1 = z1; X2 = x2; Y2 = y2; Z2 = z2;
    }
}

[XmlArray("Robot")]
public Coordinate[] points = new Coordinate[] {
    new Coordinate(1, 2, 3, 4, 5, 6),
    new Coordinate(6, 5, 4, 3, 2, 1),
    new Coordinate(2, 3, 4 ,5, 6, 7),
}

// serialize (remove long namespace)
var space = new XmlSerializerNamespaces();
space.Add("", "");
var serializer = new XmlSerializer(points.GetType()); // typeof(Coordinate[])
using (var stream = new FileStream("robot.xml", FileMode.Create))
    serializer.Serialize(stream, points, space);

// deserialize
using (var stream = new FileStream("robot.xml", FileMode.Open, FileAccess.Read))
    points = (Coordinate[])serializer.Deserialize(stream);

【讨论】:

  • 我知道有更简单的方法,但我必须使用配置文件
【解决方案2】:

试试这个方法:

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

然后用 for 循环填充你的数组:

这里是完整的代码:

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;

【讨论】:

  • 这对我不起作用。当我实现它时,它给出了很多我根本无法解决的错误
  • 使您的 xDoc 实例静态化。 "public static XDocument xDoc = .." 它应该可以工作。因为我自己尝试使用您的 xml 文档并且一切工作正常。当您看到错误时,如果您告诉我异常消息,它会很有用!
  • 我必须将 for 循环准确地放在哪里?我要删除什么?
  • 当前上下文中不存在名称“AlleCoordinaten / GetPostionValue”
  • 我试过了,但它给出了错误:''invaled token'' in vlass、struct 或 interface number 声明。所以现在我把所有东西都放在一个构造函数中(我更新了我的代码)它给出了错误:当前上下文中不存在名称'AlleCoordinaten / GetPostionValue'
猜你喜欢
  • 2012-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
相关资源
最近更新 更多