[Unity] Parsing and generating XML files

Table of contents

Parsing using XPath path syntax

Parse using xml syntax

Generation of XML files


XML files are a commonly used data exchange format that store data in text form and use tags to describe the data. Parsing and generating XML files are common tasks in software development.

Parsing XML files refers to the process of reading data from XML files. In .NET, you can use the XmlDocument class to parse XML files. The following introducestwo methods to parse XML files.

  • We use theXmlDocument class to load the XML file and use XPath expressions to select the nodes that need to be read. We can then use the Attributes attribute to get the node's properties, and the InnerText attribute to get the node's text content.
  • If the node contains child nodes, you can use theChildNodes attribute to get the collection of child nodes, and use the same method to get the attributes or text content of the child nodes.

Parsing using XPath path syntax

        //2、使用XPath语法解析 路径
        //创建xml对象
        XmlDocument xml = new XmlDocument();
        //读取xml文件
        xml.Load(Application.dataPath + "/Hokag.xml");

        // 使用XPath语法解析路径,获取所有名为"HokagInfor"的元素  
        // SelectNodes方法返回一个XmlNodeList对象,该对象包含所有匹配的元素
        XmlNodeList nodeList = xml.SelectNodes("Hokags/HokagInfor");

        // 对获取到的每个"HokagInfor"元素进行操作 
        foreach (XmlElement item in nodeList)
        {
            Hokag hokag = new Hokag();
            //获取特性
            string id = item.GetAttribute("id");
            //获取子节点的值
            hokag.Name = item.ChildNodes[0].InnerText;
            hokag.Age = Int32.Parse(item.ChildNodes[1].InnerText);
            hokag.Skill = item.ChildNodes[2].InnerText;
            Debug.Log(id + ": " + hokag);
        }

Parse using xml syntax

        //1、使用xml语法解析
        //创建xml对象
        XmlDocument xml = new XmlDocument();
        //读取xml文件
        xml.Load(Application.dataPath + "/Hokag.xml");
        //获取根节点
        XmlNode root = xml.LastChild;
        //获取根节点的子节点
        XmlNodeList nodeList = root.ChildNodes;


        foreach (XmlElement item in nodeList)
        {
            Hokag hokag = new Hokag();
            //获取特性
            string id = item.GetAttribute("id");
            //获取子节点的值
            hokag.Name = item.ChildNodes[0].InnerText;
            hokag.Age = Int32.Parse(item.ChildNodes[1].InnerText);
            hokag.Skill = item.ChildNodes[2].InnerText;
            Debug.Log(id + ": " + hokag);
        }

Parsing completed

Generation of XML files

Generating XML files refers to the process of writing data into files in XML format. In .NET, you can use the XmlDocument class to generate XML files.

Create an empty XML document using the XmlDocument class. Then, we use the CreateElement method to create the elements that need to be added to the XML document, and use the SetAttribute method to add attributes to the elements. If you need to add a child node, you can use the AppendChild method to add the child node to the parent node. Finally, we save the XML document to a file using the Save method.

  //3.生成xml
  //创建xml对象
  XmlDocument xml = new XmlDocument();
  //创建声明
  xml.AppendChild(xml.CreateXmlDeclaration("1.0", "UTF-8", null));
  //创建根节点
  XmlNode root = xml.CreateElement("Hokags");
  //设置根节点
  xml.SelectSingleNode(root.ToString());
  //添加
  xml.AppendChild(root);

  //创建根节点的子节点
  XmlElement hokagInfor = xml.CreateElement("HokagInfor");
  hokagInfor.SetAttribute("id", "1");
  //创建子节点
  XmlElement nameElement = xml.CreateElement("Name");
  XmlElement ageElement = xml.CreateElement("age");
  XmlElement skillElement = xml.CreateElement("Skill");
  nameElement.InnerText = "漩涡鸣人";
  ageElement.InnerText = "20";
  skillElement.InnerText = "嘴遁";

  hokagInfor.AppendChild(nameElement);
  hokagInfor.AppendChild(ageElement);
  hokagInfor.AppendChild(skillElement);

  root.AppendChild(hokagInfor);

  //文件保存
  xml.Save(Application.dataPath + "/HokagInformation.xml");

 Generation completed

 

Guess you like

Origin blog.csdn.net/m0_64476561/article/details/134463522