2016-10-04 71 views
1

所以,我有這樣的XML:C#XML解析 - 獲取子節點的屬性

<tileset firstgid="1" name="simple_tiles" tilewidth="32" tileheight="32" tilecount="16" columns="8"> 
    <image source="../Users/mkkek/Pictures/rpg/default_tiles_x.png" width="256" height="64"/> 
</tileset> 

當我在tileset節點,我怎麼能訪問image節點及其source屬性?我的代碼如下:

public void LoadMaps(ContentManager content) 
    { 
     Dictionary<string, string> mapsToLoad = InitMapsToLoad(); 

     foreach (KeyValuePair<string, string> mapToLoad in mapsToLoad) 
     { 
      Map map = new Map(); 
      map.Name = Path.GetFileNameWithoutExtension(mapToLoad.Value); 

      reader = XmlReader.Create("Content/" + mapToLoad.Value); 

      while(reader.Read()) 
      { 
       if(reader.NodeType == XmlNodeType.Element) 
       { 
        switch(reader.Name) 
        { 
         case "tileset": 
          if(!Tilesets.Any(ts => ts.Name == reader.GetAttribute("name"))) 
          { 
           // handling the image node here 
          } 
          break; 
        } 
       } 
      } 
     } 
    } 
+0

是否有原因使用XmlReader而沒有LINQ to XML? – YuvShap

+0

不,我應該使用它嗎?看來我無法訪問'System.Xml.Linq'命名空間。 – mkkekkonen

+0

現在我發現了,我不得不添加它作爲參考。 – mkkekkonen

回答

0

我通常喜歡用LINQ to XML,因爲我覺得它的API要容易得多比XmlReader中,該技術here之間的比較來使用。

如果你需要的是從圖像元素源獲得屬性值這很容易實現:

var doc = XDocument.Load("something.xml"); 
var root = doc.DocumentElement; 
var imageElements = root.Elements("image").ToList(); 
foreach (var imageElement in imageElements) 
{ 
    var sourceAttribute = imageElement.Attribute("source"); 
    var sourceValue = sourceAttribute.Value; 
    //do something with the source value... 
} 

更多關於LINQ to XML中here基本查詢。

0

你差不多完成了。將此添加到您的代碼中。

// handling the image node here 
if (reader.ReadToDescendant("image")) 
{ 
    string source = reader.GetAttribute("source"); 
} 
0

我會建議創建一個代表XML結構的一些類,像這樣:

[XmlRoot(ElementName = "image")] 
    public class Image 
    { 
     [XmlAttribute(AttributeName = "source")] 
     public string Source { get; set; } 
     [XmlAttribute(AttributeName = "width")] 
     public string Width { get; set; } 
     [XmlAttribute(AttributeName = "height")] 
     public string Height { get; set; } 
    } 

    [XmlRoot(ElementName = "tileset")] 
    public class Tileset 
    { 
     [XmlElement(ElementName = "image")] 
     public Image Image { get; set; } 
     [XmlAttribute(AttributeName = "firstgid")] 
     public string Firstgid { get; set; } 
     [XmlAttribute(AttributeName = "name")] 
     public string Name { get; set; } 
     [XmlAttribute(AttributeName = "tilewidth")] 
     public string Tilewidth { get; set; } 
     [XmlAttribute(AttributeName = "tileheight")] 
     public string Tileheight { get; set; } 
     [XmlAttribute(AttributeName = "tilecount")] 
     public string Tilecount { get; set; } 
     [XmlAttribute(AttributeName = "columns")] 
     public string Columns { get; set; } 
    } 

然後在一些實用工具類添加以下方法:

public static T DeserializeFromXml<T>(string xml) 
    { 
     if (string.IsNullOrEmpty(xml)) 
     { 
      return default(T); 
     } 

     var serializer = new XmlSerializer(typeof(T)); 
     T entity; 

     using (XmlReader reader = XmlReader.Create(new StringReader(xml))) 
     { 
      entity = (T)serializer.Deserialize(reader); 
     } 

     return entity; 
    } 

現在,您將能夠使用以下代碼訪問Image對象:

Tileset tileset=DeserializeFromXml<Tileset>(yourXmlContent); 
// now you can access the image from the tileset instance 'tileset.Image.Source'