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 には、ウェイポイントと呼ばれる vector2 のリストが含まれています。

私が知りたいのは、私が間違っていることです。見ているパスが変わるたびに新しいパスを作成したいのですが、見た目は問題ありません。私が混乱しているのは、次に何をすべきかということです。waypoints = (from i in path.Descendants("pathPoints") の後、私は何かをしなければならないことを期待していますが、何についてはわかりません。

どんな助けでも大歓迎です。

編集

1 つまたは 2 つの詳細を追加するのを忘れました。

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 に答える