xml文件的操作

项目中基本都会用到xml,今天整理下关于xml的一些操作,一下两个链接是比较好的参考,大家可以看看。

 

【参考】http://bbs.csdn.net/topics/90261348

【参考】http://www.cnblogs.com/Sky_KWolf/archive/2010/12/08/1899803.html

 

2 、XML 数据的查询 

最常见的XML数据类型有:Element, Attribute,Comment, Text.

Element, 指形如<Name>Tom<Name>的节点。它可以包括:

Element, Text, Comment, ProcessingInstruction, CDATA, and EntityReference. 

Attribute, 指在<Employee id=”12345”>中的粗体部分。 

Comment,指形如:<!-- my comment --> 的节点。 

Text,指在<Name>Tom<Name>的粗体部分。 

在XML中,可以用XmlNode对象来参照各种XML数据类型。 
 2.1 、查询已知绝对路径的节点(集) 

 objNodeList = objDoc.SelectNodes(“Company/Department/Employees/Employee”)或者 objNodeList = objNode.SelectNodes(“/Company/Department/Employees/Employee”) 以上两种方法可返回一个NodeList对象,如果要返回单个节点可使用SelectSingleNode方法,该方法如果查询到一个或多个节点,返回第一个节点;如果没有查询的任何节点返回 Nothing。例如: 

 objNode = objNode.SelectSingleNode(“/Company/Department/Employees/Employee”) 

 If Not (objNode is Nothing) then 

 ‘- Do process

 End If 
  2.2、 查询已知相对路径的节点(集) 

可使用类似于文件路径的相对路径的方式来查询XML的数据 

 objNode = objDoc.SelectSingleNode(“Company/Department”)

 objNodeList = objNode.SelectNodes(“../Department) 

 objNode = objNode.SelectNode(“Employees/Employee”) 
  2.3、 查询已知元素名的节点(集) 

在使用不规则的层次文档时,由于不知道中间层次的元素名,可使用//符号来越过中间的节点,查询其子,孙或多层次下的其他所有元素。例如: 

 objNodeList = objDoc.SelectNodes(“Company//Employee”) 
  2.4 、查询属性(attribute)节点 

以上的各种方法都返回元素(element)节点(集),返回属性(attribute),只需要采用相应的方法,在属性名前加一个@符号即可,例如

objNodeList = objDoc.SelectNodes(“Company/Department/Employees/Employee/@id”) 

 objNodeList = objDoc.SelectNodes(“Company//@id”) 
  2.5、 查询Text节点 

使用text()来获取Text节点。 

objNode= bjDoc.SelectSingleNode(“Company/Department/Deparmt_Name/text()”) 
  2.6、 查询特定条件的节点 

使用[]符号来查询特定条件的节点。例如: 

 a. 返回id号为 10102的Employee节点 

objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee[@id=’10102’]”) 

 b. 返回Name为Zhang Qi的Name 节点 

objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee/Name[text()=’Zhang Qi’]”) 

c. 返回部门含有职员22345的部门名称节点 

 objNode = objDoc.SelectSingleNode("Company/Department[Employees/Employee/@id='22345']/Department_Name") 
  2.7、 查询多重模式的节点 

使用 | 符号可以获得多重模式的节点。例如: 

 objNodeList = objDoc.SelectNodes(“Company/Department/Department_Name | Company/Department/Manager”) 
  2.8 、查询任意子节点 

使用*符号可以返回当前节点的所有子节点。 

objNodeList = objDoc.SelectNodes(“Company/*/Manager)  

或者 objNodeList = objNode.ChildNodes 



参考代码:

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;

public enum NodeList
{
	root,

	book,
	title,
	author,
	price,
}

public class SingleXmlWR  
{
	private static SingleXmlWR _xmlWriteRead;
	public List<string> _myXmlDoc = new List<string>();
	
	public static SingleXmlWR GetInstance
	{
		get
		{
			if(_xmlWriteRead == null)
			{
				_xmlWriteRead = new SingleXmlWR();
			}
			return _xmlWriteRead;
		}
	}
	/// <summary>
	/// 创建xml文档
	/// </summary>
	/// <returns><c>true</c>, if xml document was created, <c>false</c> otherwise.</returns>
	/// <param name="_fileName">_file name.</param>
	public bool CreatXmlDocument(string _fileName)
	{
		bool _isCreat = false;
		try
		{
			XmlDocument _xmlDoc = new XmlDocument();//XmlDocument类继承自_xmlNode类

			XmlDeclaration _xmlDeclaion = _xmlDoc.CreateXmlDeclaration("1.0","gb2312","");//表示 XML 声明节点:<?xml version='1.0'...?>。
			_xmlDoc.AppendChild(_xmlDeclaion);

			XmlNode _xmlNode = _xmlDoc.CreateElement(NodeList.root.ToString());
			_xmlDoc.AppendChild(_xmlNode);

			_xmlDoc.Save(Application.streamingAssetsPath +"/"+_fileName);

			_isCreat = true;
		}
		catch(XmlException e)
		{
			Debug.Log(e);
		}
		return _isCreat;
	}
	/// <summary>
	/// 读取xml文档
	/// </summary>
	/// <param name="_fileName">_file name.</param>
	public void ReadXmlDocument(string _fileName)
	{
		XmlDocument _xmlDoc = new XmlDocument();
		_xmlDoc.Load(Application.streamingAssetsPath +"/"+_fileName);
		XmlNode _xmlNode = _xmlDoc.SelectSingleNode(NodeList.root.ToString());

		XmlNodeList _xmlNodeList = _xmlNode.ChildNodes;

		foreach(XmlNode _xef in _xmlNodeList)
		{
			XmlElement _xe = (XmlElement) _xef;
			Debug.Log(_xe.GetAttribute("genre"));
			Debug.Log(_xe.GetAttribute("ISBN"));

			XmlNodeList _xefChild = _xe.ChildNodes;
			foreach(XmlNode _xefCld in _xefChild)
			{
				Debug.Log(_xefCld.InnerText);
			}
		} 
	}
	/// <summary>
	/// 创建节点、添加属性
	/// </summary>
	/// <returns><c>true</c>, if attribute was added, <c>false</c> otherwise.</returns>
	/// <param name="_fileName">xml文件路径</param>
	/// <param name="_node">添加的节点名</param>
	/// <param name="_genre">添加的节点genre属性.</param>
	/// <param name="_isbn">添加的节点isbn属性.</param>
	public bool AddAttribute(string _fileName, string _node,string _genre,string _isbn)
	{
		bool _isAdd = false;
		try
		{
			XmlDocument _xmlDoc = new XmlDocument();
			_xmlDoc.Load(Application.streamingAssetsPath +"/"+_fileName);

			XmlNode _root = _xmlDoc.SelectSingleNode(NodeList.root.ToString());

			XmlElement _nodeAtt = _xmlDoc.CreateElement(_node);

			_nodeAtt.SetAttribute("genre", _genre);
			_nodeAtt.SetAttribute("ISBN", _isbn);
			//_nodeAtt.GetAttribute("genre");
			_root.AppendChild(_nodeAtt);

			_xmlDoc.Save(Application.streamingAssetsPath +"/"+_fileName);
			_isAdd = false;
		}
		catch(XmlException e)
		{
			Debug.Log(e);
		}
		return _isAdd;
	}

	/// <summary>
	/// 添加节点数据
	/// </summary>
	/// <param name="_fileName">xml文件路径</param>
	/// <param name="_parentNode">添加的节点的父节点</param>
	/// <param name="_addNode">添加的新节点的名字</param>
	/// <param name="_content">添加的新节点的内容.</param>
	public void AddNodeData(string _fileName, string _parentNode,string _addNode,string _content)
	{
		XmlDocument _xmlDoc = new XmlDocument();
		_xmlDoc.Load(Application.streamingAssetsPath +"/"+_fileName);

		XmlNode _fatherNode = _xmlDoc.SelectSingleNode("root/"+_parentNode);

		XmlElement _node = _xmlDoc.CreateElement(_addNode);
		_node.InnerText = _content;

		if(_fatherNode != null)
			_fatherNode.AppendChild(_node);

		_xmlDoc.Save(Application.streamingAssetsPath +"/"+_fileName);

	}
	/// <summary>
	/// 更新节点数据
	/// </summary>
	/// <param name="_fileName">xml文件路径.</param>
	/// <param name="_nodeName">需要更新的节点的名字.</param>
	/// <param name="_attribute">需要更新的节点的父节点的属性.</param>
	/// <param name="_content">需要更新的内容</param>
	public void UpdateNodeData(string _fileName,string _nodeName,string _attribute,string _content)
	{
		XmlDocument _xmlDoc = new XmlDocument();
		_xmlDoc.Load(Application.streamingAssetsPath +"/"+_fileName);
		XmlNode _xmlNode = _xmlDoc.SelectSingleNode(NodeList.root.ToString());
		
		XmlNodeList _xmlNodeList = _xmlNode.ChildNodes;
		
		foreach(XmlNode _xef in _xmlNodeList)
		{
			XmlElement _xe = (XmlElement) _xef;

			XmlNodeList _xefChild = _xe.ChildNodes;
			foreach(XmlNode _xefCld in _xefChild )
			{
				if(_xefCld.Name == _nodeName && _xe.GetAttribute("genre") == _attribute)
				{
					_xefCld.InnerText = _content;
				}
			}
		} 
		_xmlDoc.Save(Application.streamingAssetsPath +"/"+_fileName);
	}
	/// <summary>
	/// 删去节点信息
	/// </summary>
	/// <param name="_fileName">xml文件路径</param>
	/// <param name="_removeNode">需要删除的节点.</param>
	/// <param name="_attribute">需要删除的节点的属性.</param>
	public void RemoveNodeData(string _fileName,string _removeNode,string _attribute)
	{
		XmlDocument _xmlDoc = new XmlDocument();
		_xmlDoc.Load(Application.streamingAssetsPath +"/"+_fileName);
		XmlNode _xmlNode = _xmlDoc.SelectSingleNode(NodeList.root.ToString());
		
		XmlNodeList _xmlNodeList = _xmlNode.ChildNodes;

		foreach(XmlNode _xef in _xmlNodeList)
		{
			XmlElement _xe = (XmlElement) _xef;

			if(_xe.Name ==_removeNode && _xe.GetAttribute("genre") == _attribute)
			{
				_xe.RemoveAll();
			}
		}

		_xmlDoc.Save(Application.streamingAssetsPath +"/"+_fileName);
	}
	
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值