寫一個樣例一樣的東西吧,是關(guān)于Xml文件讀取的谨履。
這是一個保衛(wèi)蘿卜的地圖笋粟。
usingUnityEngine;
usingSystem.Collections;
usingSystem.Xml;
///Xml工具類
publicclassXmlTools
{
///加載level數(shù)據(jù)
///xml路徑
///level結(jié)構(gòu)
publicstaticvoidLoadLevel(stringfilePath,refLevellevel)
{
XmlDocumentdoc =newXmlDocument();
doc.Load(filePath);//加載xml文件
level.Name = doc.SelectSingleNode("Level/Name").InnerText;
level.CardImage = doc.SelectSingleNode("Level/CardImage").InnerText;
level.Background = doc.SelectSingleNode("Level/Background").InnerText;
level.Road = doc.SelectSingleNode("Level/Road").InnerText;
level.InitScore =int.Parse(doc.SelectSingleNode("Level/InitScore").InnerText);
//遍歷Holder下面所有point子節(jié)點害捕。
XmlNodeHolder = doc.SelectSingleNode("Level/Holder");
foreach(XmlNodenodeinHolder.ChildNodes)
{
Pointp =newPoint();
p.X =int.Parse(node.Attributes["X"].Value)+1;
p.Y =int.Parse(node.Attributes["Y"].Value)+1;
level.Holder.Add(p);
}
//遍歷Round下面所有子節(jié)點闷畸。
XmlNodeRounds = doc.SelectSingleNode("Level/Rounds");
foreach(XmlNodenodeinRounds.ChildNodes)
{
Roundround =newRound();
round.Monster =int.Parse(node.Attributes["Monster"].Value);
round.Count =int.Parse(node.Attributes["Count"].Value);
level.Round.Add(round);
}
XmlNodePath = doc.SelectSingleNode("Level/Path");
foreach(XmlNodenodeinPath.ChildNodes)
{
Pointp =newPoint();
p.X =int.Parse(node.Attributes["X"].Value) + 1;
p.Y =int.Parse(node.Attributes["Y"].Value) + 1;
level.Path.Add(p);
}
}
}