我的XML文件中有数字和后缀。数字由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>
答案 0 :(得分:1)
一个有用的提示:
我相信您希望为此任务使用词典或集合,而不是列表。
如果您使用的集合不允许使用倍数,那么您不必执行那么多逻辑。您可以检查项目是否存在并添加或不添加。
答案 1 :(得分:0)
试试这个
class NumberLetter
{
public string Number {get; set;}
public string Letter {get; set;}
NumberLetter(string number, string letter)
{
Number = number;
Letter = letter;
}
}
public class AB : MonoBehaviour {
void Start () {
List<NumberLetter> myList = new List<NumberLetter>();
myList.Add(new NumberLetter("2", "a"));
myList.Add(new NumberLetter("2", "b"));
myList.Add(new NumberLetter("2", "c"));
// ...
// writing
sing (var sw = new StreamWriter("D:\\new.xml"))
{
var g = new XmlSerializer(typeof(List<NumberLetter>));
g.Serialize(sw, myList);
}
// reading
using (var sr = new StreamReader("D:\\new.xml"))
{
var l = new XmlSerializer(typeof(List<NumberLetter>));
myList = (List<NumberLetter>)l.Deserialize(sr);
}
// ...
}
}