using System;
using System.Xml;
//XML的创建
/// <summary>
/// 创建新的XML
/// </summary>
public void CreateNewXml(string updateXml)
{
XmlDocument xmldoc = new XmlDocument();//表示 XML 文档。
//xmldoc.CreateNode创建一个XmlNode节点
XmlNode xmlNode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,"","");//创建XML声明
xmldoc.AppendChild(xmlNode);
xmlNode = xmldoc.CreateNode(XmlNodeType.Element, "Root", "");
xmldoc.AppendChild(xmlNode);
XmlNode root = xmldoc.SelectSingleNode("Root");
gameelem = xmldoc.CreateElement("TestClass");
gameelem.InnerText = "v1.0";
root.AppendChild(gameelem);
xmldoc.Save(updateXml);
}
XML的写入:
/// <summary>
/// 向新的XML中写入信息, updateName参数是需要更新的文件对象,updateNamePath参数是更新对象的相对路径
/// </summary>
/// <param name="updateName"></param>
/// <param name="updateNamePath"></param>
public void WriteToNewXml(string updateName, string updateNamePath,string updateXml)
{
XmlDocument xd = new XmlDocument();
xd.Load(updateXml);
XmlNode root = xd.DocumentElement;
XmlElement objectInfo = xd.CreateElement("objectInfo");
objectInfo.SetAttribute("genre", updateNamePath);
objectInfo.SetAttribute("isDir", "0");
XmlElement updateObject = xd.CreateElement("updateObject");
updateObject.InnerText = updateName;
objectInfo.AppendChild(updateObject);
XmlElement updateObjectPath = xd.CreateElement("updateObjectPath");
updateObjectPath.InnerText = updateNamePath;
objectInfo.AppendChild(updateObjectPath);
XmlElement updateObjectServerPath = xd.CreateElement("updateObjectServerPath");//创建一个节点
updateObjectServerPath.InnerText = "url";
objectInfo.AppendChild(updateObjectServerPath);
xd.DocumentElement.AppendChild(objectInfo);//添加到节点中
xd.Save(updateXml);
}
XML的读取:
/// <summary>
/// 从newXml 中读取新的XML ,gameRootPath是对游戏制作索引的路径
/// </summary>
/// <param name="gameRootPath"></param>
/// <param name="newXml"></param>
public void ReadFromNewXML(string gameRootPath,string newXml)
{
XmlDocument xdReadNewXml = new XmlDocument();
xdReadNewXml.Load(newXml);//从磁盘加载到内存
XmlNodeList xnReadNewXml = xdReadNewXml.SelectNodes("//objectInfo");//查找
string makeIndexDirPath = gameRootPath + "\\" + "update";
string DirFullPath = "";
foreach (XmlNode xnObjectInfo in xnReadNewXml)//遍历所有子节点
{
XmlElement xeObjectInfo = (XmlElement)xnObjectInfo;//将子节点类型转换为XmlElement类型 DirFullPath = "";
makeIndexDirPath = gameRootPath + "\\" + "update";
if (xeObjectInfo.GetAttribute("isDir") == "1")//读取isDir属性值
{
DirFullPath = gameRootPath + "\\" + "new" + xeObjectInfo.GetAttribute("genre");
string SameFatherDir = xeObjectInfo.GetAttribute("genre");
makeIndexDirPath += xeObjectInfo.GetAttribute("genre");
if (File.Exists(makeIndexDirPath))//判断文件是否存在
{
continue;
}
else
{
CopyFilesDirs(DirFullPath, makeIndexDirPath);
}
}
else
{
string FileFullPath = gameRootPath + xeObjectInfo.GetAttribute("genre");
FileFullPath += ("\\"+ xeObjectInfo["updateObject"].InnerText);
makeIndexDirPath += xeObjectInfo.GetAttribute("genre");//读取genre属性
createdir(makeIndexDirPath);
saveFile(makeIndexDirPath, FileFullPath);
}
}
}