如果是只有一种结构的节点,则取到node节点数组,然后遍历即可
例:
private void readXml(string xmlPath)
{
if (!File.Exists(xmlPath))
{
Debug.LogError("file does not exist");
return;
}
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
XmlNodeList nodeList = doc.GetElementsByTagName("item");
foreach(XmlNode node in nodeList)
{
int price = int.Parse(node.Attributes["price"].Value);
string name = node.Attributes["name"].Value;
}
}
如果是有多种结构的节点,则需要单独取每个节点解析
例:
private void readXml(string xmlPath)
{
if (!File.Exists(xmlPath))
{
Debug.LogError("file does not exist");
return;
}
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
XmlNode root = doc.SelectSingleNode("root");
XmlNode logonData = root.SelectSingleNode("logonData");
string userName = logonData.Attributes["userName"].Value;
string pwd = logonData.Attributes["pwd"].Value;
}