XMLTools

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

///
/// XML助手类
///
public class XMLTools
{

/// 
/// 获取根节点
/// 
/// 
/// 
public static string[] GetXMLOneElement(string path)
{

    XmlNodeList xmlList = GetRoot(path, "Root");

    List listele = new List();

    foreach (XmlElement item in xmlList)
    {

        listele.Add(item.Name);
    }

    return listele.ToArray();
}



/// 
/// 获取所有节点
/// 
/// 
/// 
public static XmlNodeList GetRoot(string path, string name)
{

    XmlDocument xmlDocument = new XmlDocument();

    xmlDocument.Load(path);

    XmlNodeList element = xmlDocument.SelectSingleNode(name).ChildNodes;

    return element;

}




/// 
/// 获取二级节点下的任意一级
/// 
/// 
/// 
/// 
public static string[] GetXMLElement(string path, string name)
{

    XmlNodeList elements = GetRoot(path, name);

    List xmlList = new List();

    foreach (XmlElement item in elements)
    {
        xmlList.Add(item.Name);
     //   Debug.Log(item.Name);
    }

    return xmlList.ToArray();
}


/// 
/// 获取属性value
/// 
/// 
/// 
public static string[] GetXMLAttribute(string path, string name)
{

    XmlDocument xmlDocument = new XmlDocument();

    xmlDocument.Load(path);

    List xmlList = new List();

    XmlAttributeCollection xmlAttributeCollection = xmlDocument.SelectSingleNode(name).Attributes;

    foreach (XmlAttribute item in xmlAttributeCollection)
    {
        xmlList.Add(item.Value);
        Debug.Log(item.Name);
    }

    return xmlList.ToArray();
}



/// 
/// 获取属性Key
/// 
/// 
/// 
public static string[] GetXMLAttributeKey(string path, string name)
{

    XmlDocument xmlDocument = new XmlDocument();

    xmlDocument.Load(path);

    List xmlList = new List();

    XmlAttributeCollection xmlAttributeCollection = xmlDocument.SelectSingleNode(name).Attributes;

    foreach (XmlAttribute item in xmlAttributeCollection)
    {
        xmlList.Add(item.Name);
        Debug.Log(item.Name);
    }

    return xmlList.ToArray();
}



/// 
/// 获取属性XmlAttributeCollection
/// 
/// 
/// 
/// 
public static XmlAttributeCollection GetXMLAttributeColl(string path, string name)
{

    XmlDocument xmlDocument = new XmlDocument();

    xmlDocument.Load(path);

    XmlAttributeCollection xmlAttributeCollection = xmlDocument.SelectSingleNode(name).Attributes;


    return xmlAttributeCollection;
}

}

你可能感兴趣的:(XMLTools)