using UnityEngine;
using UnityEditor;
using System.Xml;
public class JsonTools :Editor
{
[MenuItem("Mytools/XMLRun")]
public static void XNLRun() {
// 写入
XmlDocument doc = new XmlDocument();
XmlElement player= doc.CreateElement("Player");
player.SetAttribute("id", "1231123");
// 将元素放入文档
doc.AppendChild(player);
XmlElement name= doc.CreateElement("name");
player.AppendChild(name);
name.InnerText = "李四";
XmlElement level = doc.CreateElement("level");
player.AppendChild(level);
level.InnerText = "12";
doc.Save(Application.dataPath+"/xml.txt");
AssetDatabase.Refresh();
Debug.Log(doc.InnerXml);
// 读取
doc.Load(Application.dataPath + "/xml.txt");
XmlNode playerxmlNode= doc.SelectSingleNode("Player");
string playerxmlNodeid = playerxmlNode.Attributes["id"].InnerText; // 读取属性
Debug.Log(playerxmlNodeid);
XmlNode namexmlNode = playerxmlNode.SelectSingleNode("name");
string strname = namexmlNode.InnerText;
Debug.Log(strname);
XmlNode levelxmlNode = playerxmlNode.SelectSingleNode("level");
string strlevel = levelxmlNode.InnerText;
Debug.Log(strlevel);
}
}
xml 写入本地保存应用配置
using System;
using System.IO;
using System.Xml;
using Test.Road.Log.Log_msg;
using Test.Road.Tools.Singleton_Tool;
namespace Test.Road.RWConfig
{
public enum ConfigData
{
DeviceId, // 站点ID
IP, // IP地址
Port, // 端口号
cbxCOM, // 串口号
BaudRate, // 波特率
DataBits, // 数据位
StopBits, // 停止位
Parity, // 奇偶校验
StartAuto, // 开机自启动
CharShow, // 字符显示
HEXShow, // 字符显示
Saveed, // 是否自动保存
SavePath, // 自动保存的路径
AutoSendIP, // 自动发送IP
AutoSendPort,//自动发送端口
AutoSendFilePth,
}
class ConfigManager:Singleton<ConfigManager>
{
/// <summary>
/// 配置信息写入路径
/// </summary>
string Configdatapath = AppDomain.CurrentDomain.BaseDirectory + "配置文件\\";
string fileName = "config.xml";
string ConfigFullname;
/// <summary>
///创建配置文件xml
/// </summary>
public void Create()
{
ConfigFullname = Configdatapath + fileName;
if (!Directory.Exists(Configdatapath))
Directory.CreateDirectory(Configdatapath);
XmlDocument xd = new XmlDocument();
// 判断配置信息保存路径是否存在 不存在创建
if (File.Exists(ConfigFullname))
xd.Load(ConfigFullname);
else // 存在路径及文件 进行写入
{
//创建xml 头文件
XmlDeclaration xdt = xd.CreateXmlDeclaration("1.0", "utf-8", null);
xd.AppendChild(xdt);
// 创建唯一跟节点
XmlElement WinData = xd.CreateElement("WinData");
// 给根节点添加属性
WinData.SetAttribute("name", "保存xx设置");
xd.AppendChild(WinData);
// 创建二级根节点
XmlElement winData = xd.CreateElement("xx信息");
// winData.SetAttribute("串口设置", "");
WinData.AppendChild(winData);
xd.Save(ConfigFullname);
}
}
/// <summary>
/// 读取配置文件信息
/// </summary>
/// <returns></returns>
public XmlNode ReadData()
{
try
{
XmlDocument xd = new XmlDocument();
xd.Load(ConfigFullname);
XmlNode Node = xd.SelectSingleNode("WinData/xx信息");
return Node;
}
catch (Exception e)
{
LogMSG.WriteLogmsg("存储界面信息错误","ReadData " +e.Message.ToString());
}
return null;
}
/// <summary>
/// 当配置信息改变调用刷新数据
/// </summary>
/// <param name="Nodename"></param>
/// <param name="NodeValue"></param>
/// <param name="Attributevalie"></param>
public void DataChange(ConfigData Nodename,string NodeValue, string Attributevalie ="")
{
try
{
XmlDocument xd = new XmlDocument();
xd.Load(ConfigFullname);
if (xd != null)
{
XmlNode Node = xd.SelectSingleNode("WinData/xx信息");
XmlNode _Nodename = Node.SelectSingleNode(Nodename.ToString());
if (_Nodename == null)
{
XmlElement __Nodename = xd.CreateElement(Nodename.ToString());
__Nodename.SetAttribute("name", Attributevalie);
__Nodename.InnerText = NodeValue;
Node.AppendChild(__Nodename);
}
else {
_Nodename.InnerText = NodeValue;
}
xd.Save(ConfigFullname);
}
else {
ReadData();
XmlNode Node = xd.SelectSingleNode("WinData/xx信息");
XmlNode _Nodename = Node.SelectSingleNode(Nodename.ToString());
XmlElement __Nodename = xd.CreateElement(Nodename.ToString());
__Nodename.SetAttribute("name", Attributevalie);
__Nodename.InnerText = NodeValue;
Node.AppendChild(__Nodename);
LogMSG.WriteLogmsg("存储界面信息错误","DataChange 没有加载到文件" );
}
}
catch (Exception e)
{
LogMSG.WriteLogmsg("存储界面信息错误","DataChange " +e.Message.ToString());
}
}
}
}