从XML文件中读取数组

目前我得到了这个:

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

  1 2 3 4 5 6 etc... 7  

它仍然无法正常工作,你们可以帮我解决我做错的事情并举一个例子。

使用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); 

试试这个方法:

 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;