[英]C# get a list of all elements with the same name - XML Deserialize
I try to get a list of all sound elements in the XML-File. 我尝试获取XML文件中所有声音元素的列表。
I got all marker but when i try to get all elements with the tag sound i don't get anything. 我得到了所有标记但是当我试图获得标签声音的所有元素时,我什么也得不到。 The list is always 0.
该列表始终为0。
Root Class 根类
[XmlArray("markers")]
[XmlArrayItem("marker")]
public List<WaypointInfo> markers = new List<WaypointInfo>();
[XmlArray("pattern")]
[XmlArrayItem("sound")]
public List<SoundInfo> soundsInfo = new List<SoundInfo> ();
/// <summary>
/// The file extension.
/// </summary>
public static readonly string fileExtensionMp3 = "*.mp3";
public static FileLoader loadInformationXML(string filePath){
//Define the rootclass
XmlSerializer serializer = new XmlSerializer (typeof(FileLoader));
//Create inputstream
FileStream fl = new FileStream (filePath, FileMode.Open);
//Extract the stream and save it into our root class
FileLoader xmlData = serializer.Deserialize (fl) as FileLoader;
//Close the inputstream
fl.Close ();
return xmlData;
}
The markers arraylist is correct but the other list of sounds is always null. 标记arraylist是正确的,但另一个声音列表始终为空。
Class WaypointsInfo 类WaypointsInfo
using UnityEngine;
using System.Collections.Generic;
using System.Xml.Serialization;
public class WaypointInfo {
[XmlAttribute("name")]
public string markerName;
public Pattern pattern = new Pattern();
}
Class Soundinfo Soundinfo类
using UnityEngine;
using System.Xml.Serialization;
using System.Collections;
public class SoundInfo {
/// <summary>
/// The minimum distance that triggers this sound.
/// </summary>
[XmlAttribute("distance")]
public float distance;
/// <summary>
/// When this sound has been played the last time, in runtime milliseconds.
/// </summary>
public float lastPlayed;
/// <summary>
/// If this sound should be looped.
/// </summary>
public bool looped;
}
and the xml file 和xml文件
<soundpackage>
<markers>
<marker name="bird">
<pattern name="aPattern">
<sound distance="10">bird.mp3</sound>
<sound distance="10">bird.mp3</sound>
</pattern>
</marker>
</markers>
</soundpackage>
Does your root node have code like this xmlns=http;//somepath
? 您的根节点是否具有类似
xmlns=http;//somepath
? Then you are dealing with namespaces. 然后你正在处理命名空间。 Namespaces have to be considered when you are getting zero results and you are sure you are referencing it correctly.
当您获得零结果并且您确定正确引用它时,必须考虑命名空间。
I'm not familiar with Xml Serialization. 我不熟悉Xml序列化。 So I looked up namespaces for XML Serialization.
所以我查找了XML序列化的名称空间。 Here's an example on another StackOverflow thread.
这是另一个StackOverflow线程的示例。
See this answer for a serialization answer with namespaces. 有关命名空间的序列化答案,请参阅此答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.