2012-09-03 61 views
1

我想要像這樣讀取XML。使用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稱爲航點的列表。

我想知道的是我做錯了什麼。我想在每次改變它正在查看的路徑時創建一個新路徑,這看起來很好。然而我感到困惑的是下一步該做什麼。航點=(從我在path.Descendants(「pathPoints」),我很期待後,我必須做一些事情,但我無能,什麼。

任何幫助,將不勝感激。

。編輯

一個或兩個細節我忘了補充

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

我們不知道你的'AIPath'類型是什麼樣的,這使得它很難提供幫助。另外,你還沒有告訴我們你的* current *代碼是幹什麼的。 (看起來它基本上是一個語法錯誤......) –

+0

此外,當前似乎試圖將'point'元素解析爲* single *整數,當它清楚地包含* two *整數時。你能夠改變XML格式嗎?它看起來會更清潔,如果這兩個值被設置爲「點」元素的屬性... –

+0

@JonSkeet是的,我可以改變格式,我需要得到X和Y.它更多地說明了什麼我以前做過。我已經添加了我的AI路徑類。它目前基本上不起作用,因爲我無法計算出需要添加的內容以告訴xml解析器將所找到的所有路徑點元素添加到AIPath路徑點表中。 – Bushes

回答

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(); 
+0

好吧,我會放棄一下。非常感謝。 – Bushes

1

喬恩斯基特的回答可能會工作,但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進口商和內容管道將構建當XML元素以正確命名的類的成員相關聯的處理器。假設你已經正確標記了資產類型元素。

參見:http://msdn.microsoft.com/en-us/library/ff604981.aspx

+0

如果您希望通過元素/屬性名稱更多地控制XML文件的序列化,則使用'[XmlAttribute]'和[[XmlElement]'也很有幫助。 –