我的 XML 文件中有数字和后者。Numbers 由 4 个项目组成,但可以增加,后者由一批后者组成,我需要做的就是从 Numbers 中获取每个项目并从 Latters 分配给它的后者。
我在下面提出了一个解决方案,但没有任何成功,任何解决它的帮助将不胜感激:)
class aWithItsB{
//It means that nextA stores 1 number and
//nextLattersBs stores latters for each number according XML file.
public string nextA;
public List<string> nextLattersBs = new List<string>();
}
public class AB : MonoBehaviour {
void Start () {
XmlDocument doc = new XmlDocument();
doc.Load("D:\\new.xml");
string lastNumber="";
XmlNodeList numbers = doc.SelectNodes("AB/AandB/@numbers");
XmlNodeList latters = doc.SelectNodes("AB/AandB/@latters");
foreach(XmlNode number in numbers){
if(number.Value!=lastNumber){
foreach(XmlNode latter in latters){
aWithItsBs AandB = new aWithItsB();
AandB.nextA = number.Value;
AandB.nextLattersB.Add(latter.Value);
print(number.Value + "" + latter.Value);
}
}
lastNumber = number.Value;
}
}
}
XML 文件:
<?xml version="1.0" encoding="iso-8859-1"?>
<AB>
<AandB numbers ="1" latters = "a"></AandB>
<AandB numbers ="1" latters = "b"></AandB>
<AandB numbers ="1" latters = "c"></AandB>
<AandB numbers ="1" latters = "c"></AandB>
<AandB numbers ="2" latters = "b"></AandB>
<AandB numbers ="2" latters = "x"></AandB>
<AandB numbers ="3" latters = "y"></AandB>
<AandB numbers ="3" latters = "a"></AandB>
<AandB numbers ="3" latters = "z"></AandB>
<AandB numbers ="4" latters = "y"></AandB>
<AandB numbers ="4" latters = "x"></AandB>
<AandB numbers ="4" latters = "a"></AandB>
</AB>