2016-10-04 13 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を使用する理由はありますかXMLへのLINQ? – YuvShap

+0

いいえ、私はそれを使うべきですか?私は 'System.Xml.Linq'名前空間にアクセスできないようです。 – mkkekkonen

+0

今、私はそれを見つけました、私は参照として追加する必要がありました。 – mkkekkonen

答えて

0

私は通常、私はそれがXmlReaderを、技術hereとの比較よりもより使いやすくなるようにAPIだ見つけるためLINQ to XMLを使用することを好みます。

あなたが必要なのは、これは容易に達成することができる画像要素からソース属性値を取得している場合:XML hereにLINQでの基本的なクエリについて

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... 
} 

詳しいです。

0

あなたはほぼ完了です。これをコードに追加します。

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

私はこのような何か、XML構造を表しているいくつかのクラスを作成することをお勧めします:今、あなたができるようになります

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; 
    } 

[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; } 
    } 

その後、いくつかのユーティリティクラスでは、次のメソッドを追加します次のコードを使用してImageオブジェクトにアクセスします。

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