using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
///
/// XML助手类
///
public class XMLTools
{
/// <summary>
/// 获取根节点
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string[] GetXMLOneElement(string path)
{
XmlNodeList xmlList = GetRoot(path, "Root");
List<string> listele = new List<string>();
foreach (XmlElement item in xmlList)
{
listele.Add(item.Name);
}
return listele.ToArray();
}
/// <summary>
/// 获取所有节点
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static XmlNodeList GetRoot(string path, string name)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNodeList element = xmlDocument.SelectSingleNode(name).ChildNodes;
return element;
}
/// <summary>
/// 获取二级节点下的任意一级
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string[] GetXMLElement(string path, string name)
{
XmlNodeList elements = GetRoot(path, name);
List<string> xmlList = new List<string>();
foreach (XmlElement item in elements)
{
xmlList.Add(item.Name);
// Debug.Log(item.Name);
}
return xmlList.ToArray();
}
/// <summary>
/// 获取属性value
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
public static string[] GetXMLAttribute(string path, string name)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
List<string> xmlList = new List<string>();
XmlAttributeCollection xmlAttributeCollection = xmlDocument.SelectSingleNode(name).Attributes;
foreach (XmlAttribute item in xmlAttributeCollection)
{
xmlList.Add(item.Value);
Debug.Log(item.Name);
}
return xmlList.ToArray();
}
/// <summary>
/// 获取属性Key
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
public static string[] GetXMLAttributeKey(string path, string name)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
List<string> xmlList = new List<string>();
XmlAttributeCollection xmlAttributeCollection = xmlDocument.SelectSingleNode(name).Attributes;
foreach (XmlAttribute item in xmlAttributeCollection)
{
xmlList.Add(item.Name);
Debug.Log(item.Name);
}
return xmlList.ToArray();
}
/// <summary>
/// 获取属性XmlAttributeCollection
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <returns></returns>
public static XmlAttributeCollection GetXMLAttributeColl(string path, string name)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlAttributeCollection xmlAttributeCollection = xmlDocument.SelectSingleNode(name).Attributes;
return xmlAttributeCollection;
}
}