C#XML生成与解析

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

public class XmlPrase : MonoBehaviour 
{	
	public class FileInfo
	{
		public string md5;
		
		public long size;
		
		public int level;
		
		public FileInfo(string _md5, long _size, int _level)
		{
			this.md5 = _md5;
			this.size = _size;
			this.level = _level;
		}
		
		public FileInfo()
		{
		}
		
		public void CopyData(FileInfo otherData)
		{
			this.md5 = otherData.md5;
			this.size = otherData.size;
			this.level = otherData.level;
		}
	}
	private Dictionary< string, FileInfo > _localResDic = new Dictionary<string, FileInfo>();
	void OnEnable()
	{
		XmlPrase.ReadVersionFileToDic( Application.dataPath + "/Resources/update.info", _localResDic );
		Debug.Log( "\n" + _localResDic.Count);
		FileStream fs = new FileStream(Application.dataPath + "/Resources/update.txt", FileMode.Create);
		StreamWriter sw = new StreamWriter(fs);

		foreach (KeyValuePair<string,FileInfo> file in _localResDic) 
		{
			//开始写入
			sw.Write(file.Key);
			sw.Write( ' ' );
			sw.Write(file.Value.md5);
			sw.Write( ' ' );
			sw.Write(file.Value.size);
			sw.Write( ' ' );
			sw.Write(file.Value.level);
			sw.Write( '\n' );
		}

		//清空缓冲区
		sw.Flush();
		//关闭流
		sw.Close();
		fs.Close();
	}

	public static bool ReadVersionFileToDic(string versionPath, Dictionary<string, FileInfo> curDic)
	{
		bool result;
		try
		{
			if (!File.Exists(versionPath))
			{
				result = false;
			}
			else
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(versionPath);
				XmlNode xmlNode = null;
				foreach (XmlNode xmlNode2 in xmlDocument.ChildNodes)
				{
					if (xmlNode2.Name == "FileList")
					{
						xmlNode = xmlNode2;
						break;
					}
				}
				if (xmlNode == null)
				{
					result = false;
				}
				else
				{
					foreach (XmlNode xmlNode3 in xmlNode.ChildNodes)
					{
						FileInfo fileInfo = new FileInfo();
						string text = null;
						foreach (XmlNode xmlNode4 in xmlNode3.ChildNodes)
						{
							if (xmlNode4.Name == "md5")
							{
								fileInfo.md5 = xmlNode4.InnerText;
							}
							else if (xmlNode4.Name == "size")
							{
								long.TryParse(xmlNode4.InnerText, out fileInfo.size);
							}
							else if (xmlNode4.Name == "path")
							{
								text = xmlNode4.InnerText;
							}
						}
						if (text != null)
						{
							curDic.Add(text, fileInfo);
						}
					}
					result = true;
				}
			}
		}
		catch (Exception ex)
		{
			result = false;
		}
		return result;
	}

	public static bool GenerateResFileList(string path, Dictionary<string, UpdateHelper.FileInfo> dicFileInfo)
		{
			bool result;
			try
			{
				Utils.CheckTargetPath(path);
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.AppendChild(xmlDocument.CreateComment("mtlbb update file"));
				XmlElement xmlElement = xmlDocument.CreateElement("FileList");
				xmlDocument.AppendChild(xmlElement);
				foreach (string current in dicFileInfo.Keys)
				{
					XmlElement xmlElement2 = xmlDocument.CreateElement("FileInfo");
					XmlElement xmlElement3 = xmlDocument.CreateElement("path");
					xmlElement3.AppendChild(xmlDocument.CreateTextNode(current));
					xmlElement2.AppendChild(xmlElement3);
					XmlElement xmlElement4 = xmlDocument.CreateElement("md5");
					xmlElement4.AppendChild(xmlDocument.CreateTextNode(dicFileInfo[current].md5));
					xmlElement2.AppendChild(xmlElement4);
					XmlElement xmlElement5 = xmlDocument.CreateElement("size");
					xmlElement5.AppendChild(xmlDocument.CreateTextNode(dicFileInfo[current].size.ToString()));
					xmlElement2.AppendChild(xmlElement5);
					XmlElement xmlElement6 = xmlDocument.CreateElement("level");
					xmlElement6.AppendChild(xmlDocument.CreateTextNode(dicFileInfo[current].level.ToString()));
					xmlElement2.AppendChild(xmlElement6);
					xmlElement.AppendChild(xmlElement2);
				}
				xmlDocument.Save(path);
				result = true;
			}
			catch (Exception ex)
			{
				LogModule.ErrorLog(ex.ToString(), new object[0]);
				result = false;
			}
			return result;
		}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值