1

我想像这样阅读 XML。

<?xml version="1.0" encoding="utf-8" ?>
<parent>
  <path>
    <pathPoints>
     <point>
        2 0
      </point>
      <point>
        2 1
      </point>
        3 1
      <point>
        3 2
      </point>
      <point>
        4 2
      </point>
      <point>
        4 4
      </point>
      <point>
        3 4
      </point>
      <point>
        3 5
      </point>
      <point>
      2 5
      </point>
      <point>
        2 7
      </point>
      <point>
          7 7
      </point>
      <point>
          7 5
      </point>
      <point>
        10 5
      </point>
      <point>
        10 2
      <point>
      </point>
      15 2
      </point>
      <point>
        15 6
      </point>
      <point>
        16 6
      </point>
      <point>
        16 7
      </point>
      <point>
        17 7
      </point>
      <point>
        17 10
      </point>
      <point>
        19 10
      </point>
    </pathPoints>
  </parent>
</path>

现在我正在尝试阅读它:

        paths = (from path in doc.Descendants("path")
                 select new AIPath()
                 {
                     waypoints = (from i in path.Descendants("pathPoints")
                     {
                         int j = Convert.ToInt32(i.Element("point").Value),
                     }).ToList();
                 }
                 ).ToList();

我的 AIPath 包含一个名为 waypoints 的 vector2 列表。

我想知道的是我做错了什么。我想在每次更改它正在查看的路径时创建一个新路径,这看起来不错。不过,我感到困惑的是下一步该做什么。在路点 =(来自路径中的 i 。后代(“路径点”)之后,我期待我必须做点什么,但我对什么一无所知。

任何帮助将不胜感激。

编辑

我忘了添加一两个细节。

public class AIPath
{
    //public Vector2
    public List<Vector2> waypoints { get; set; }
    public int linkNumber { get; set; }
    public int[] potentialLinks { get; set; }
}
4

2 回答 2

1

目前,您的 XML 输出相对难以解析。我会像这样重写你的代码:

public sealed class AIPath
{
    // TODO: Consider trying to make this immutable, or
    // at least not exposing the collections so widely. Exposing an array
    // property is almost always a bad idea.
    public List<Vector2> Waypoints { get; set; }
    public int LinkNumber { get; set; }
    public int[] PotentialLinks { get; set; }

    public XElement ToElement()
    {
        return new XElement("path",
            WayPoints.Select(v2 => new XElement("point",
                                       new XAttribute("X", (int) v2.X),
                                       new XAttribute("Y", (int) v2.Y))));
    }

    public static AIPath FromXElement(XElement path)
    {
        return new AIPath
        {
            WayPoints = path.Elements("point")
                            .Select(p => new Vector2((int) p.Attribute("X"),
                                                     (int) p.Attribute("Y")))
                            .ToList();
        };
    }
}

然后:

paths = doc.Descendants("path")
           .Select(x => AIPath.FromXElement(x))
           .ToList();
于 2012-09-03T20:33:10.137 回答
1

Jon Skeet 的回答可能会奏效,但 XNA 框架提供了一种更简单的方法,可以通过内容管道将 XML 读入您的对象。

示例 XML:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent>
  <Asset Type="MyDataTypes.CatData[]">
    <Item>
      <Name>Rhys</Name>
      <Weight>17</Weight>
      <Lives>9</Lives>
    </Item>
    <Item>
      <Name>Boo</Name>
      <Weight>11</Weight>
      <Lives>5</Lives>
    </Item>
  </Asset>
</XnaContent>

示例类:

namespace MyDataTypes
{
    public class CatData
    {
        public string Name;
        public float Weight;
        public int Lives;
    }
}

假设您已正确标记资产类型元素,则内容管道的内置 XML 导入器和处理器将在构建 .xnb 文件时将 XML 元素与正确命名的类成员相关联。

请参阅:http: //msdn.microsoft.com/en-us/library/ff604981.aspx

于 2012-09-04T17:03:02.050 回答