using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using UnityEngine;
public class ReadXmlFile
{
/// <summary>
/// XML 文档实例
/// </summary>
public XmlDocument xmlDocument = new XmlDocument();
public static string [] names;
public ReadXmlFile()
{
xmlDocument = new XmlDocument();
}
public ReadXmlFile(string filePath)
{
// xmlDocument 没实例化成功
// if (xmlDocument == null)
// {
// throw new ArgumentNullException("xmlDocument");
// }
// 给全局变量赋值
// mFilePath = filePath;
// 文件不存在
// if (!File.Exists(filePath))
// {
// // 创建一个声明
XmlDeclaration dec = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
// 把声明添加到子节点下面去
xmlDocument.AppendChild(dec);
//
// // 创建一个名字为:root的元素
// XmlElement createRoot = xmlDocument.CreateElement(mRootNodeName);;
//
// // 把元素添加到子节点下面去
// xmlDocument.AppendChild(createRoot);
//
// // 保存数据
// Save();
// }
// 文件存在
if (File.Exists(filePath))
{
// 加载 XML 数据
xmlDocument.Load(filePath);
}
}
/// <summary>
/// 根节点
/// </summary>
//XmlNode rootNode = null;
//
#region 数据加载
/// <summary>
/// 加载指定路径的xml文件
/// </summary>
// public bool Load(string filePath)
// {
// MainProcess.result="开始载入";
// if (xmlDocument == null)
// {
// MainProcess.result="为空";
// throw new ArgumentNullException("xmlDocument");
// }
//
// if (filePath == null || filePath.Length == 0)
// {
// MainProcess.result="路径为空";
// throw new ArgumentNullException("filePath");
// }
// MainProcess.result="file";
//
// MainProcess.result="开始文件";
// xmlDocument.Load(filePath);
// MainProcess.result="文件存在";
//
//
//
// return true;
// }
/// <summary>
/// 加载 XML的文本文件
/// </summary>
/// <param name="xmlFile"></param>
public bool LoadXml(string xmlFile)
{
if (xmlDocument == null)
{
throw new ArgumentNullException("xmlDocument");
}
if (xmlFile == null || xmlFile.Length == 0)
{
throw new ArgumentNullException("xmlFile");
}
TextReader tr = new StringReader(xmlFile);
// Debug.Log(tr);
xmlDocument.Load(tr);
return true;
}
#endregion
public XmlNode getRootNode()
{
return xmlDocument.DocumentElement;
}
public XmlNode getNode(string nodeName)
{
return xmlDocument.DocumentElement.SelectSingleNode(nodeName);
}
public string[] getChildNodes()
{
try
{
XmlNodeList childNodeList = xmlDocument.DocumentElement.ChildNodes;
int length = childNodeList.Count;
string[]names = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)
{
names[i] = xn.Name;
i++ ;
}
return names;
}
catch(Exception ex)
{
Debug.LogError("error: getChildNodes:"+"第二级节点名");
return new string[0];
}
}
//获取第2级所有子节点名称;
public string[] getChildNodes(string secondLevel)
{
try
{
XmlNodeList childNodeList = xmlDocument.DocumentElement.SelectSingleNode(secondLevel).ChildNodes;
int length = childNodeList.Count;
string[]names = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)
{
names[i] = xn.Name;
i++ ;
}
return names;
}
catch(Exception ex)
{
Debug.LogError("error: getChildNodes:"+secondLevel);
return new string[0];
}
}
//获取第所有第3级子节点名称;
public string[] getChildNodes(string secondLevel,string thirdLevel)
{
try
{
XmlNodeList childNodeList = (xmlDocument.DocumentElement.SelectSingleNode(secondLevel)).SelectSingleNode(thirdLevel).ChildNodes;
int length = childNodeList.Count;
string[]names = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)
{
names[i] = xn.Name;
i++ ;
}
return names;
}
catch(Exception ex)
{
Debug.LogWarning("error: getChildNodes:"+secondLevel +","+thirdLevel);
return new string[0];
}
}
//获取所有4级子节点名称;
public XmlNodeList getChildNodes(string secondLevel,string thirdLevel,string fourthLevel)
{
XmlNodeList childNodeList = (xmlDocument.DocumentElement.SelectSingleNode(secondLevel)).SelectSingleNode(thirdLevel).SelectSingleNode(fourthLevel).ChildNodes;
int length = childNodeList.Count;
// string[]names = new string[length];
// int i = 0;
// foreach(XmlNode xn in childNodeList)
// {
// names[i] = xn.Name;
// i++ ;
// }
return childNodeList;
}
//获取第三级所以子节点值,(默认root 为第一级,root下的子节点是第二级)
public string[] getAllValue(string secondLevel,string thirdLevel)
{
try
{
//获取第四级下所有子节点
XmlNodeList childNodeList = (xmlDocument.DocumentElement.SelectSingleNode(secondLevel)).SelectSingleNode(thirdLevel).ChildNodes;
int length = childNodeList.Count;//得到子节点的个数
string[]values = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)//遍历子节点列表,获得每个子节点元素值
{
values[i] = xn.InnerXml;
i++ ;
}
return values;
}
catch(Exception ex)
{
Debug.LogError("error: getAllValue:"+secondLevel +","+thirdLevel);
return new string[0];
}
}
//获取第四级所以子节点值,(默认root 为第一级,root下的子节点是第二级)
public string[] getAllValue(string secondLevel,string thirdLevel,string fourthLevel)
{
try
{
//获取第四级下所有子节点
XmlNodeList childNodeList = (xmlDocument.DocumentElement.SelectSingleNode(secondLevel)).SelectSingleNode(thirdLevel).SelectSingleNode(fourthLevel).ChildNodes;
int length = childNodeList.Count;//得到子节点的个数
string[]values = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)//遍历子节点列表,获得每个子节点元素值
{
values[i] = xn.InnerXml;
i++ ;
}
return values;
}
catch(Exception ex)
{
Debug.LogError("error: getAllValue:"+secondLevel +","+thirdLevel+","+fourthLevel);
return new string[0];
}
}
//获取第二级节点的值
public string getValue(string secondLevel)
{
try
{
XmlNode childNode = xmlDocument.DocumentElement.SelectSingleNode(secondLevel);
return childNode.InnerText;
}
catch(Exception ex)
{
// Debug.LogError("error: getValue: "+secondLevel);
return "";
}
}
//获取第三级节点的值
public string getValue(string secondLevel,string thirdLevel)
{
try
{
XmlNode childNode = xmlDocument.DocumentElement.SelectSingleNode(secondLevel).SelectSingleNode(thirdLevel);
return childNode.InnerText;
}
catch(Exception ex)
{
Debug.LogWarning("error: getValue: "+secondLevel+","+ thirdLevel);
return "";
}
}
//获取第四级节点的值
public string getValue(string secondLevel,string thirdLevel,string fourthLevel)
{
try
{
XmlNode childNode = xmlDocument.DocumentElement.SelectSingleNode(secondLevel).SelectSingleNode(thirdLevel).SelectSingleNode(fourthLevel);
return childNode.InnerText;
}
catch(Exception ex)
{
Debug.LogError("error: getValue: "+secondLevel+","+ thirdLevel+", "+fourthLevel);
return "";
}
}
//获取第五级节点的值
public string getValue(string secondLevel,string thirdLevel,string fourthLevel,string fifthLevel)
{
try
{
XmlNode childNode = xmlDocument.DocumentElement.SelectSingleNode(secondLevel).SelectSingleNode(thirdLevel).SelectSingleNode(fourthLevel).SelectSingleNode(fifthLevel);
return childNode.InnerText;
}
catch(Exception ex)
{
Debug.LogError("error: getValue: "+secondLevel+", "+ thirdLevel+", "+fourthLevel+", "+fifthLevel);
return "";
}
}
public string[] findNodeInfo(string attribute,string parentName)
{
XmlElement xn = null;
XmlElement targetElement = null;
for(int i = 0;i<xmlDocument.DocumentElement.ChildNodes.Count;i++)
{
XmlElement xe = (XmlElement)xmlDocument.DocumentElement.ChildNodes[i];
if(xe.GetAttribute("objName").Equals(parentName))
{
xn = xe;
}
}
for(int i = 0;i<xn.ChildNodes.Count;i++)
{
XmlElement xe = (XmlElement)xn.ChildNodes[i];
if(xe.GetAttribute("type").Equals("node"))
{
for(int j = 0;j<xe.ChildNodes.Count;j++)
{
XmlElement xt = (XmlElement)xe.ChildNodes[j];
if(xt.GetAttribute("type").Equals("node"))
{
for(int k = 0;k<xt.ChildNodes.Count;k++)
{
XmlElement xk = (XmlElement)xt.ChildNodes[k];
if(xk.GetAttribute("type").Equals("node"))
{
for(int h = 0;h<xk.ChildNodes.Count;h++)
{
XmlElement xh = (XmlElement)xk.ChildNodes[h];
if(xh.GetAttribute("objName").Equals(attribute))
{
targetElement =xh;
goto stop;
//break;
}
}
}
else
{
if(xk.GetAttribute("objName").Equals(attribute))
{
targetElement = xk;
goto stop;
//break;
}
}
}
}
else
{
// Debug.Log(xt.GetAttribute("objName"));
if(xt.GetAttribute("objName")==attribute)
{
targetElement = xt;
goto stop;
//break;
}
}
}
}
else
{
//Debug.Log(xe.GetAttribute("objName"));
if(xe.GetAttribute("objName").Equals(attribute))
{
targetElement = xe;
goto stop;
//break;
}
}
}
stop:;
//文字,图片,视频信息
string [] info = new string[10];
if(targetElement!=null)
{
info[0] = targetElement.GetAttribute("图片路径");
info[1] = targetElement.GetAttribute("声音路径");
info[2] = targetElement.GetAttribute("视频路径");
info[3] = targetElement.Name;
int ii = 4;
XmlNode node = targetElement.ParentNode;
string name = "";
if(Application.loadedLevelName.Equals("Project"))
{
name = "女性人体解剖";
}
else
{
name = "男性人体解剖";
}
while(!node.Name.Equals(name))
{
info[ii] = node.Name;
node = node.ParentNode;
ii++;
}
//获取简介信息
// HumanList.txtInfo = targetElement.InnerText;
}
return info;
}
public void Save(string mFilePath)
{
// xmlDocument 没实例化成功
if (xmlDocument == null)
{
throw new ArgumentNullException("xmlDocument");
}
// 路径为空
if (mFilePath == null || mFilePath.Length == 0)
{
throw new ArgumentNullException("路径名为空");
}
// 如果文件存在
if (File.Exists(mFilePath))
{
// 删除原来的文件
File.Delete(mFilePath);
}
// 保存数据到指定的路径
xmlDocument.Save(mFilePath);
}
public void GetAttrbutes(XmlNode nd,List<string> attrlist)
{
for(int i = 0;i<nd.ChildNodes.Count;i++)
{
attrlist.Add(((XmlElement)nd.ChildNodes[i]).GetAttribute("chineseName"));
//Debug.Log(((XmlElement)nd.ChildNodes[i]).GetAttribute("chineseName"));
}
}
}
using System.IO;
using System.Xml;
using System.Collections.Generic;
using UnityEngine;
public class ReadXmlFile
{
/// <summary>
/// XML 文档实例
/// </summary>
public XmlDocument xmlDocument = new XmlDocument();
public static string [] names;
public ReadXmlFile()
{
xmlDocument = new XmlDocument();
}
public ReadXmlFile(string filePath)
{
// xmlDocument 没实例化成功
// if (xmlDocument == null)
// {
// throw new ArgumentNullException("xmlDocument");
// }
// 给全局变量赋值
// mFilePath = filePath;
// 文件不存在
// if (!File.Exists(filePath))
// {
// // 创建一个声明
XmlDeclaration dec = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
// 把声明添加到子节点下面去
xmlDocument.AppendChild(dec);
//
// // 创建一个名字为:root的元素
// XmlElement createRoot = xmlDocument.CreateElement(mRootNodeName);;
//
// // 把元素添加到子节点下面去
// xmlDocument.AppendChild(createRoot);
//
// // 保存数据
// Save();
// }
// 文件存在
if (File.Exists(filePath))
{
// 加载 XML 数据
xmlDocument.Load(filePath);
}
}
/// <summary>
/// 根节点
/// </summary>
//XmlNode rootNode = null;
//
#region 数据加载
/// <summary>
/// 加载指定路径的xml文件
/// </summary>
// public bool Load(string filePath)
// {
// MainProcess.result="开始载入";
// if (xmlDocument == null)
// {
// MainProcess.result="为空";
// throw new ArgumentNullException("xmlDocument");
// }
//
// if (filePath == null || filePath.Length == 0)
// {
// MainProcess.result="路径为空";
// throw new ArgumentNullException("filePath");
// }
// MainProcess.result="file";
//
// MainProcess.result="开始文件";
// xmlDocument.Load(filePath);
// MainProcess.result="文件存在";
//
//
//
// return true;
// }
/// <summary>
/// 加载 XML的文本文件
/// </summary>
/// <param name="xmlFile"></param>
public bool LoadXml(string xmlFile)
{
if (xmlDocument == null)
{
throw new ArgumentNullException("xmlDocument");
}
if (xmlFile == null || xmlFile.Length == 0)
{
throw new ArgumentNullException("xmlFile");
}
TextReader tr = new StringReader(xmlFile);
// Debug.Log(tr);
xmlDocument.Load(tr);
return true;
}
#endregion
public XmlNode getRootNode()
{
return xmlDocument.DocumentElement;
}
public XmlNode getNode(string nodeName)
{
return xmlDocument.DocumentElement.SelectSingleNode(nodeName);
}
public string[] getChildNodes()
{
try
{
XmlNodeList childNodeList = xmlDocument.DocumentElement.ChildNodes;
int length = childNodeList.Count;
string[]names = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)
{
names[i] = xn.Name;
i++ ;
}
return names;
}
catch(Exception ex)
{
Debug.LogError("error: getChildNodes:"+"第二级节点名");
return new string[0];
}
}
//获取第2级所有子节点名称;
public string[] getChildNodes(string secondLevel)
{
try
{
XmlNodeList childNodeList = xmlDocument.DocumentElement.SelectSingleNode(secondLevel).ChildNodes;
int length = childNodeList.Count;
string[]names = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)
{
names[i] = xn.Name;
i++ ;
}
return names;
}
catch(Exception ex)
{
Debug.LogError("error: getChildNodes:"+secondLevel);
return new string[0];
}
}
//获取第所有第3级子节点名称;
public string[] getChildNodes(string secondLevel,string thirdLevel)
{
try
{
XmlNodeList childNodeList = (xmlDocument.DocumentElement.SelectSingleNode(secondLevel)).SelectSingleNode(thirdLevel).ChildNodes;
int length = childNodeList.Count;
string[]names = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)
{
names[i] = xn.Name;
i++ ;
}
return names;
}
catch(Exception ex)
{
Debug.LogWarning("error: getChildNodes:"+secondLevel +","+thirdLevel);
return new string[0];
}
}
//获取所有4级子节点名称;
public XmlNodeList getChildNodes(string secondLevel,string thirdLevel,string fourthLevel)
{
XmlNodeList childNodeList = (xmlDocument.DocumentElement.SelectSingleNode(secondLevel)).SelectSingleNode(thirdLevel).SelectSingleNode(fourthLevel).ChildNodes;
int length = childNodeList.Count;
// string[]names = new string[length];
// int i = 0;
// foreach(XmlNode xn in childNodeList)
// {
// names[i] = xn.Name;
// i++ ;
// }
return childNodeList;
}
//获取第三级所以子节点值,(默认root 为第一级,root下的子节点是第二级)
public string[] getAllValue(string secondLevel,string thirdLevel)
{
try
{
//获取第四级下所有子节点
XmlNodeList childNodeList = (xmlDocument.DocumentElement.SelectSingleNode(secondLevel)).SelectSingleNode(thirdLevel).ChildNodes;
int length = childNodeList.Count;//得到子节点的个数
string[]values = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)//遍历子节点列表,获得每个子节点元素值
{
values[i] = xn.InnerXml;
i++ ;
}
return values;
}
catch(Exception ex)
{
Debug.LogError("error: getAllValue:"+secondLevel +","+thirdLevel);
return new string[0];
}
}
//获取第四级所以子节点值,(默认root 为第一级,root下的子节点是第二级)
public string[] getAllValue(string secondLevel,string thirdLevel,string fourthLevel)
{
try
{
//获取第四级下所有子节点
XmlNodeList childNodeList = (xmlDocument.DocumentElement.SelectSingleNode(secondLevel)).SelectSingleNode(thirdLevel).SelectSingleNode(fourthLevel).ChildNodes;
int length = childNodeList.Count;//得到子节点的个数
string[]values = new string[length];
int i = 0;
foreach(XmlNode xn in childNodeList)//遍历子节点列表,获得每个子节点元素值
{
values[i] = xn.InnerXml;
i++ ;
}
return values;
}
catch(Exception ex)
{
Debug.LogError("error: getAllValue:"+secondLevel +","+thirdLevel+","+fourthLevel);
return new string[0];
}
}
//获取第二级节点的值
public string getValue(string secondLevel)
{
try
{
XmlNode childNode = xmlDocument.DocumentElement.SelectSingleNode(secondLevel);
return childNode.InnerText;
}
catch(Exception ex)
{
// Debug.LogError("error: getValue: "+secondLevel);
return "";
}
}
//获取第三级节点的值
public string getValue(string secondLevel,string thirdLevel)
{
try
{
XmlNode childNode = xmlDocument.DocumentElement.SelectSingleNode(secondLevel).SelectSingleNode(thirdLevel);
return childNode.InnerText;
}
catch(Exception ex)
{
Debug.LogWarning("error: getValue: "+secondLevel+","+ thirdLevel);
return "";
}
}
//获取第四级节点的值
public string getValue(string secondLevel,string thirdLevel,string fourthLevel)
{
try
{
XmlNode childNode = xmlDocument.DocumentElement.SelectSingleNode(secondLevel).SelectSingleNode(thirdLevel).SelectSingleNode(fourthLevel);
return childNode.InnerText;
}
catch(Exception ex)
{
Debug.LogError("error: getValue: "+secondLevel+","+ thirdLevel+", "+fourthLevel);
return "";
}
}
//获取第五级节点的值
public string getValue(string secondLevel,string thirdLevel,string fourthLevel,string fifthLevel)
{
try
{
XmlNode childNode = xmlDocument.DocumentElement.SelectSingleNode(secondLevel).SelectSingleNode(thirdLevel).SelectSingleNode(fourthLevel).SelectSingleNode(fifthLevel);
return childNode.InnerText;
}
catch(Exception ex)
{
Debug.LogError("error: getValue: "+secondLevel+", "+ thirdLevel+", "+fourthLevel+", "+fifthLevel);
return "";
}
}
public string[] findNodeInfo(string attribute,string parentName)
{
XmlElement xn = null;
XmlElement targetElement = null;
for(int i = 0;i<xmlDocument.DocumentElement.ChildNodes.Count;i++)
{
XmlElement xe = (XmlElement)xmlDocument.DocumentElement.ChildNodes[i];
if(xe.GetAttribute("objName").Equals(parentName))
{
xn = xe;
}
}
for(int i = 0;i<xn.ChildNodes.Count;i++)
{
XmlElement xe = (XmlElement)xn.ChildNodes[i];
if(xe.GetAttribute("type").Equals("node"))
{
for(int j = 0;j<xe.ChildNodes.Count;j++)
{
XmlElement xt = (XmlElement)xe.ChildNodes[j];
if(xt.GetAttribute("type").Equals("node"))
{
for(int k = 0;k<xt.ChildNodes.Count;k++)
{
XmlElement xk = (XmlElement)xt.ChildNodes[k];
if(xk.GetAttribute("type").Equals("node"))
{
for(int h = 0;h<xk.ChildNodes.Count;h++)
{
XmlElement xh = (XmlElement)xk.ChildNodes[h];
if(xh.GetAttribute("objName").Equals(attribute))
{
targetElement =xh;
goto stop;
//break;
}
}
}
else
{
if(xk.GetAttribute("objName").Equals(attribute))
{
targetElement = xk;
goto stop;
//break;
}
}
}
}
else
{
// Debug.Log(xt.GetAttribute("objName"));
if(xt.GetAttribute("objName")==attribute)
{
targetElement = xt;
goto stop;
//break;
}
}
}
}
else
{
//Debug.Log(xe.GetAttribute("objName"));
if(xe.GetAttribute("objName").Equals(attribute))
{
targetElement = xe;
goto stop;
//break;
}
}
}
stop:;
//文字,图片,视频信息
string [] info = new string[10];
if(targetElement!=null)
{
info[0] = targetElement.GetAttribute("图片路径");
info[1] = targetElement.GetAttribute("声音路径");
info[2] = targetElement.GetAttribute("视频路径");
info[3] = targetElement.Name;
int ii = 4;
XmlNode node = targetElement.ParentNode;
string name = "";
if(Application.loadedLevelName.Equals("Project"))
{
name = "女性人体解剖";
}
else
{
name = "男性人体解剖";
}
while(!node.Name.Equals(name))
{
info[ii] = node.Name;
node = node.ParentNode;
ii++;
}
//获取简介信息
// HumanList.txtInfo = targetElement.InnerText;
}
return info;
}
public void Save(string mFilePath)
{
// xmlDocument 没实例化成功
if (xmlDocument == null)
{
throw new ArgumentNullException("xmlDocument");
}
// 路径为空
if (mFilePath == null || mFilePath.Length == 0)
{
throw new ArgumentNullException("路径名为空");
}
// 如果文件存在
if (File.Exists(mFilePath))
{
// 删除原来的文件
File.Delete(mFilePath);
}
// 保存数据到指定的路径
xmlDocument.Save(mFilePath);
}
public void GetAttrbutes(XmlNode nd,List<string> attrlist)
{
for(int i = 0;i<nd.ChildNodes.Count;i++)
{
attrlist.Add(((XmlElement)nd.ChildNodes[i]).GetAttribute("chineseName"));
//Debug.Log(((XmlElement)nd.ChildNodes[i]).GetAttribute("chineseName"));
}
}
}