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;
}
}
C#XML生成与解析
最新推荐文章于 2024-05-22 14:29:31 发布