XML的结构分析+创建

XML是用于传输数据的一种结构。所以我把它看做是数据结构的方式。

构成方式为 :元素 + 元素值 + (元素的)属性 + 属性的值

例如:

             <Alarm  lock="true">

                    Ming

                    <Time>

                          StringValue

                     </Time>

           </Alarm>

结构分析:

                   1. Alarm  为元素  : lock 为元素的属性,“True”为属性的值

                   2.Alarm元素的值 : Ming。
 
                   3.元素Time为Alarm元素的子元素

                   4.元素Time的值为StringVlue,但是却没有Time的属性

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//导入Xml命名空间
using System.Xml;

public class CreateXMLScripts : MonoBehaviour {
    private void Start()
    {
        CreateXml();
    }

    void CreateXml()
    {
        /* 继承关系
         -System.Object 
          -System.Xml.XmlNode(表示XML节点)
            +System.Xml.XmlDocument  (表示XML文档)
            +System.Xml.XmlAttribute  (表示XML属性)
            -System.Xml.XmlLinkedNode 
              +System.Xml.XmlElement(表示XML元素)        
         */
        //创建Xml文件
        XmlDocument Doc = new XmlDocument();
 //1.为此Xml文件创建文件声明:申明版本,编码格式,
             // standalone :The value must be either "yes" or "no". If this is null or String.Empty, the Save method does not write a standalone attribute on the XML declaration.
             //独立的:这个值必须是“Yes”或“No”;Null 或者string.Empty都标示为"No",那么Save函数便不会在XMl的声明中写入独立的属性
             // EnCoding :编码格式默认为:UTF-8 ;还有UTF-16
        XmlDeclaration declaration = Doc.CreateXmlDeclaration("1.0", "utf-8",null);
        //添加声明
        Doc.AppendChild(declaration);
//2.创建第一个元素:即为根元素
        XmlElement TransformNode = Doc.CreateElement("Transform");
            //为元素创建属性
            XmlAttribute attributeOfTransform = Doc.CreateAttribute("解释");
            attributeOfTransform.Value = "游戏对象的变换";
            //添加属性
            TransformNode.Attributes.SetNamedItem(attributeOfTransform);
            //把根元素添加到文件中
            Doc.AppendChild(TransformNode);
//3.为TransformNode创建子元素
        XmlElement PositionNode = Doc.CreateElement("Position");
            //元素属性
        XmlAttribute attributeOfPosition = Doc.CreateAttribute("解释");
        attributeOfPosition.Value = "游戏对象的位置";
           //添加属性  :
        PositionNode.Attributes.SetNamedItem(attributeOfPosition);
           //直接添加属性  PositionNode.SetAttribute("解释", "游戏对象的位置");
           //把positionNode元素添加为Transform的子元素
        TransformNode.AppendChild(PositionNode);
 //4.为position创建子元素,包含的是坐标点
        XmlNode xPosNode = Doc.CreateElement("X");
        xPosNode.InnerText = "23";
        PositionNode.AppendChild(xPosNode);
        XmlNode yPosNode = Doc.CreateElement("Y");
        yPosNode.InnerText = "3";
        PositionNode.AppendChild(yPosNode);
        XmlNode zPosNode = Doc.CreateElement("Z");
        zPosNode.InnerText = "2";
        PositionNode.AppendChild(zPosNode);
//5.保存
        Doc.Save(Application.dataPath + "/Xml的创建" + "/myXml.xml");

    }

}

当然这是效果:

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,184评论 6 13
  • 1. XML简介 以下内容来自于http://www.w3school.com.cn/xml 基本知识 XML 和...
    WebSSO阅读 1,965评论 1 7
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 最近每天都在看小郭分享的心灵巨人,关于责任的一些文章,感触比较深的是,逃避责任,找一些借口都是错误的,当我们遇事后...
    Ding欣欣阅读 354评论 0 0
  • 刚刚,就在刚刚,打开潮汐的番茄钟 戴着耳机,静静听海浪的声音 结果发现, 自己压根就没把耳机插到手机里 今天对自己...
    大品品阅读 463评论 0 0