您的位置:首页 > 编程语言 > C#

C#解析XML

2015-08-06 10:41 429 查看
如果是只有一种结构的节点,则取到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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: