XML文件操作--读取

<pre name="code" class="html"><span style="font-family: Arial,Helvetica,sans-serif;"><?xml version="1.0" encoding="utf-8" ?></span>
<root> <testmark mark="3" /> <test0 a="a0" b="b0" c="c0"/> <test1 a="a1" b="b1" c="c1"/> <test2 a="a2" b="b2" c="c2"/> <test3 a="a3" b="b3" c="c3"/></root>
 

上面的XML保存在Unity项目如下目录中:

Application.dataPath + "/res/XMLFile1.xml"


文件:using System.Xml;

读取配置:
    private void loadXML1()
    {
        //Debug.LogError(Application.dataPath + "/res/XMLFile1.xml");

        XmlDocument doc = new XmlDocument();
        doc.Load(Application.dataPath + "/res/XMLFile1.xml");

        XmlNode root = doc.DocumentElement;//获取XML的根

        XmlNode nodemark = root.SelectSingleNode("testmark");
        string strmark = nodemark.Attributes["mark"].Value;

        XmlNode node = root.SelectSingleNode(string.Format("test{0}",strmark));
        Debug.LogError(string.Format("node.Attributes[\"a\"].Value = {0},node.Attributes[\"b\"].Value = {1},node.Attributes[\"c\"].Value = {2}",node.Attributes["a"].Value,node.Attributes["b"].Value,node.Attributes["c"].Value));
    }

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念