C#XML解析-子节点的Get属性
因此,我有如下XML:C#XML解析-子节点的Get属性,c#,xml,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 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>
你差不多完成了。将此添加到您的代码中
// handling the image node here
if (reader.ReadToDescendant("image"))
{
string source = reader.GetAttribute("source");
}
你差不多完成了。将此添加到您的代码中
// handling the image node here
if (reader.ReadToDescendant("image"))
{
string source = reader.GetAttribute("source");
}
我通常更喜欢使用,因为我发现它的API比XmlReader更容易使用,这是两种技术之间的比较
如果您只需要从图像元素获取源属性值,则可以轻松实现:
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中的基本查询的信息 我通常更喜欢使用它,因为我发现它的API比XmlReader更容易使用,这是两种技术之间的比较
如果您只需要从图像元素获取源属性值,则可以轻松实现:
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中的基本查询的信息 我建议创建一些表示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;
}
publicstatict反序列化fromXML(字符串xml)
{
if(string.IsNullOrEmpty(xml))
{
返回默认值(T);
}
var serializer=newxmlserializer(typeof(T));
T实体;
使用(XmlReader=XmlReader.Create(新的StringReader(xml)))
{
实体=(T)序列化程序。反序列化(读取器);
}
返回实体;
}
现在,您可以使用以下代码访问图像对象:
Tileset tileset=DeserializeFromXml<Tileset>(yourXmlContent);
// now you can access the image from the tileset instance 'tileset.Image.Source'
Tileset Tileset=DeserializeFromXml(yourXmlContent);
//现在,您可以从tileset实例“tileset.image.Source”访问图像
我建议创建一些表示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;
}
publicstatict反序列化fromXML(字符串xml)
{
if(string.IsNullOrEmpty(xml))
{
返回默认值(T);
}
var serializer=newxmlserializer(typeof(T));
T实体;
使用(XmlReader=XmlReader.Create(新的StringReader(xml)))
{
实体=(T)序列化程序。反序列化(读取器);
}
返回实体;
}
现在,您可以使用以下代码访问图像对象:
Tileset tileset=DeserializeFromXml<Tileset>(yourXmlContent);
// now you can access the image from the tileset instance 'tileset.Image.Source'
Tileset Tileset=DeserializeFromXml(yourXmlContent);
//现在,您可以从tileset实例“tileset.image.Source”访问图像
使用XmlReader而不是LINQ to XML有什么原因吗?不,我应该使用它吗?似乎我无法访问System.Xml.Linq
名称空间。现在我找到了它,我必须将它添加为引用。您使用XmlReader而不是Linq to Xml有什么原因吗?不,我应该使用它吗?似乎我无法访问System.Xml.Linq
名称空间。现在我找到了它,我不得不添加它作为引用。