unity读写XML的相关操作

读写主要用到了

XmlDocument 类

XmlDocument.GetElementsByTagName 方法 (String)

返回一个  XmlNodeList ,它包含与指定  Name  匹配的所有子代元素的列表。

using System;
using System.Xml;
using UnityEngine;
class CarXMLConfigure
{
	public const string Root="Root";
	public const string NetworkConfigure="NetworkConfigure";
	public const string IP="IP";
	public const string FileName="config.xml";
	public const string StreamingAssets="/StreamingAssets/";
    public const string NM = "nm";
    
}



class XMLManagement
{
	private XmlDocument doc;
	private XmlElement root;
	private string fileName;

	public XMLManagement()
	{
		doc=new XmlDocument();
	}

    //首先加载
	public bool LoadFile(string filePath)
	{
		fileName=filePath;

		doc.Load(filePath);
		if(doc.InnerXml!="")
		{
			root=doc.DocumentElement;
			return true;
		}
		else
			return false;
	}


	public void CreateXMLFile(string filePath) //name should contain .xml,for example "config.xml"
	{
		fileName = filePath;
		XmlDeclaration xmldecl;		
		xmldecl = doc.CreateXmlDeclaration("1.0","gb2312",null);		
		doc.AppendChild (xmldecl);

		root=doc.CreateElement(CarXMLConfigure.Root);
		doc.AppendChild(root);
	}
    //添加节点
	public void setAttribute(string attriName,string attriValue)
	{
		XmlElement child=doc.CreateElement(CarXMLConfigure.NetworkConfigure);
		child.SetAttribute(attriName,attriValue);
		if(root==null)
		{
			Console.WriteLine("the root is null,please create it or get it from an exist xml file!");
			return;
		}
		root.AppendChild(child);
        save();//添加节点的时候 需要保存

    }

	public void save()
	{
		doc.Save(fileName);
	}
    //  获取节点,
	public string getIP()
	{
        //  
        XmlNodeList list = root.GetElementsByTagName(CarXMLConfigure.NetworkConfigure); //root.SelectNodes(CarXMLConfigure.NetworkConfigure);//

        string text="";
		foreach(XmlNode node in list)
		{
           
            XmlElement xmlnode = (XmlElement)node;
            Debug.Log(node.Value + node.InnerXml + " ==  " + xmlnode.Name+" == "+xmlnode.Value +" === "+node.LocalName);
            text = xmlnode.GetAttribute("IP11");//  如果跟节点相同。只能获取到第二个。。。
		}
        Debug.Log(list.Count+" "+text);
		return text;
	}
    public string getAaaryIP(int  a)
    {
        XmlNodeList list = root.GetElementsByTagName(CarXMLConfigure.NetworkConfigure + a.ToString());

        string text = "";
        foreach (XmlNode node in list)
        {
            text = ((XmlElement)node).GetAttribute(CarXMLConfigure.IP+a.ToString());
        }
        //Debug.Log(text + "  " + CarXMLConfigure.IP + a.ToString());
        return text;
       
    }
    public string getMus() {
        XmlNodeList list = root.GetElementsByTagName("MusConfig");
        string text = "";
        foreach (XmlNode node in list) {
            text = ((XmlElement)node).GetAttribute("Mus");
        }

        return text;
    }
    public string getNum() {
        XmlNodeList list = root.GetElementsByTagName("OthConfig");
        string text = "";
        foreach (XmlNode node in list)
        {
            text = ((XmlElement)node).GetAttribute(CarXMLConfigure.NM);
        }
        return text;

    }
    public string getN()
    {
        XmlNodeList list = root.GetElementsByTagName("NetConfig");
        string text = "";
        foreach (XmlNode node in list)
        {
            text = ((XmlElement)node).GetAttribute("m");
        }
        return text;

    }
    public string getNam() {

        XmlNodeList list = root.GetElementsByTagName("NamConfig");
        string text = "";
        foreach (XmlNode node in list) {

            text = ((XmlElement)node).GetAttribute("nam");
        }
        return text;

    }
    public string GetIP(string s) {
        XmlNodeList list = root.GetElementsByTagName(s);
        string text = "";
        foreach (XmlNode node in list) {
            text = ((XmlElement)node).GetAttribute(s);
        }
        return text;
    }

    public string GetIp_Name(int a)
    {
        XmlNodeList list = root.GetElementsByTagName(CarXMLConfigure.NetworkConfigure + a.ToString());

        string text = "";
        foreach (XmlNode node in list)
        {
            text = ((XmlElement)node).GetAttribute(CarXMLConfigure.IP + a.ToString());
            text = text + "|" + ((XmlElement)node).GetAttribute("name"+a.ToString());
            text = text + "|" + ((XmlElement)node).GetAttribute("num"+a.ToString());
        }
        //Debug.Log(text + "  " + CarXMLConfigure.IP + a.ToString());
        return text;

    }
}

然后在其他地方调用就也可以了

using UnityEngine;
using System.Collections;

public class ReadXML : MonoBehaviour {
    XMLManagement manager;
	// Use this for initialization
	void Start () {
        manager = new XMLManagement();
        manager.LoadFile(Application.dataPath+CarXMLConfigure.StreamingAssets+CarXMLConfigure.FileName);
        string str = manager.getAaaryIP(1); //manager.getIP();
        string str1 = manager.getIP();
        manager.setAttribute("A","B");// 添加
        Debug.Log(str +"  ===  "+str1);
	}
	
	// Update is called once per frame
	void Update () {
	    
	}
}


///  面向对象的三大特征:封装,继承, 多态
父类指针指向子类对象,然后调用父类方法



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值