zoukankan      html  css  js  c++  java
  • XML 创建

    using unityEngine;
    
    using System.Collections;
    
    using System.Linq;
    
    using System.Xml.Linq;
    
    using System;
    
    
    
    public class XML {
    
    //static string xmlpath = Application.persistentDataPath + @"myXML";//平台相关的路径(移动端)
    
    static string xmlpath=Application.dataPath+@"mydfdfXML";//电脑上的路径,移动端没有这个访问权限
    
    /// <summary>
    
    /// 初始化一个XML文件
    
    /// </summary>
    
    public static void CreateXMLDocument()
    
    {
    
    XElement root = new XElement("XMLContent",
    
    new XElement("Herb1",new XAttribute("MyVaule","0")),
    
    new XElement("Herb2",new XAttribute("MyVaule","0")),
    
    new XElement("Herb3",new XAttribute("MyVaule","0")),
    
    new XElement("Pill1",new XAttribute("MyVaule","0")),
    
    new XElement("Pill2",new XAttribute("MyVaule","0")),
    
    new XElement("Pill3",new XAttribute("MyVaule","0")),
    
    new XElement("Level",new XAttribute("MyVaule","0")),
    
    new XElement("Root","root")
    
    );
    
    root.Save(xmlpath);
    
    }
    
    public static XElement LoadXMLFromFile()
    
    {
    
    XElement root = XElement.Load(xmlpath);
    
    return root;
    
    }
    
    public static void SetElementValue(string name, string value)
    
    {
    
    XElement root = LoadXMLFromFile();
    
    root.Element(name).SetAttributeValue("MyVaule", value);
    
    root.Save(xmlpath);
    
    }
    
    /// <summary>
    
    /// 在根节点元素之前添加新的元素
    
    /// </summary>
    
    /// <param name="name">元素名字</param>
    
    /// <param name="value">元素的值</param>
    
    public static void AddElement(string name, string value)
    
    {
    
    XElement root = LoadXMLFromFile();
    
    root.Element("Root").AddBeforeSelf(new XElement(name, new XAttribute("MyValue",value)));
    
    root.Save(xmlpath);
    
    }
    
    /// <summary>
    
    /// 删除指定的元素
    
    /// </summary>
    
    /// <param name="name">要删除的元素名称</param>
    
    public static void RemoveElement(string name)
    
    {
    
    XElement root = LoadXMLFromFile();
    
    root.Element(name).Remove();
    
    root.Save(xmlpath);
    
    }
    
    /// <summary>
    
    /// 根据元素名查找元素对应的值
    
    /// </summary>
    
    /// <param name="name">元素名</param>
    
    /// <returns></returns>
    
    public static string GetElementValue(string name)
    
    {
    
    XElement root = LoadXMLFromFile();
    
    XAttribute xattr = root.Element(name).Attribute("MyVaule");
    
    string s = xattr.Value;
    
    return s;
    
    }
    
    }
  • 相关阅读:
    OC面向对象—封装
    设计模式之类关系
    理性:中国别一厢情愿救俄罗斯(转)
    Mockito--完整功能介绍(转)
    从陌陌上市看BAT的移动保卫战(转)
    This exception may occur if matchers are combined with raw values
    RepositoryClassLoader.java
    搭建你的持续集成server
    mysql中怎样查看和删除唯一索引
    Android中Context具体解释 ---- 你所不知道的Context
  • 原文地址:https://www.cnblogs.com/123ing/p/3843966.html
Copyright © 2011-2022 走看看