C#XML解析 - 获取子节点的属性

时间:2016-10-04 00:55:08

标签: c# xml

所以,我有这样的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;
                    }
                }
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

你差不多完成了。将其添加到您的代码中。

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

答案 1 :(得分:0)

我通常更喜欢使用LINQ to XML因为我发现它的API比XmlReader更容易使用,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中基本查询的更多信息。

答案 2 :(得分: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; }
    }

然后在某些Utility类中添加以下方法:

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'