2016-10-04 2 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

나는 보통 나는 그것이 API는 XmlReader를, 기술 here 사이의 비교보다 사용하기 훨씬 쉽게 수의 발견 때문에 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'