在XML中存储数组/使用LINQ读取它

时间:2013-05-01 14:54:16

标签: c# xml arrays linq

我正在尝试使用LINQ获取表示特定城市之间距离的2D数组。我应该选择适当的XML表示问题。起初,我使用MatLAB样式的数组来表示依赖关系,如下所示:

<cityMap>
      [-, 2, 3, 4, 5, 6, 7, 8; 
          1, -, 3, 4, 5, 6, 7, 8;
          1, 2, -, 4, 5, 6, 7, 8;
          1, 2, 3, -, 5, 6, 7, 8;
          1, 2, 3, 4, -, 6, 7, 8;
          1, 2, 3, 4, 5, -, 7, 8;
          1, 2, 3, 4, 5, 6, -, 8;
          1, 2, 3, 4, 5, 6, 7, -]
</cityMap>

从单个LINQ查询中获取此矩阵并在C#中解析它非常方便,但从XML的角度来看,它是不正确的(一个标记中有多个数据)。现在我正在尝试这样的事情:

<salesman>
  <salesmanNumber>10</salesmanNumber>
  <cities>
    <city id="1">
      <headquarter>1</headquarter>
      <distance toCity="2">2</distance>
      <distance toCity="3">3</distance>
      <distance toCity="4">4</distance>
      <distance toCity="5">5</distance>
      <distance toCity="6">6</distance>
    </city>
    <city id="2">
      <headquarter>1</headquarter>
      <distance toCity="1">1</distance>
      <distance toCity="3">3</distance>
      <distance toCity="4">4</distance>
      <distance toCity="5">5</distance>
      <distance toCity="6">6</distance>
               [....]
  </cities>
</salesman>

出现了两个问题:  1.这是表示数组的正确形式吗?  2.如何使用LINQ查询读取此数据,以便我能够     将其解析为数组?

1 个答案:

答案 0 :(得分:0)

它需要是XML吗?二维数组更自然地映射到Excel文件

相关问题