Unity解析Xml方法—
使用Unity自带的接口解析Xml数据
<?xml version="1.0"?>
<LoginList>
<Login name="老大" sex="男" id="0" />
<Login name="老二" sex="女" id="1" />
<Login name="老三" sex="女" id="2" />
</LoginList>
解析上文中Xml的解析Demo如下:
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System;
using System.Xml.Serialization;
public class Demo
{
public LoginList m_Logininfo;
/// <summary>
/// 用于解析xml的方法
/// </typeparam>
/// <param name="path">xml文件的路径</param>
/// <param name="ms">用于接受xml文件解析的结果</param>
public void LoadXml<T>(string path,ref T ms) where T :class
{
try
{
//通过流根据路径去读取文件
using(TextReader reader = new StreamReader(path))
{
var serializer = new XmlSerializer(typeof(T));
var item = (T)serializer.Deserialize(reader);
if (item!= null)
{
ms = item;
}
}
}
catch(Exception ex)
{
//用于捕捉异常避免解析过程中出现错误导致程序死掉
//将错误打印出来
Debug.LogError(ex.Message);
}
}
//用于接收Xml数据结构的定义规则
/*
* 用于接收数据的类上面必须接上属性标签否则属性不会被识别
* 用于接收赋值的必须定义为属性,不可为变量否则也是无法被识别的
* 定义的结构要与xml相对应
*/
[XmlRoot("LoginList")]
public class LoginList
{
[XmlElement("Login")]
public Login[] Ins{set;get;}
}
public class Login
{
[XmlAttribute(AttributeName = "name")]
public string name{set;get;}
[XmlAttribute(AttributeName = "sex")]
public string sex{set;get;}
[XmlAttribute(AttributeName = "id")]
public string id{set;get;}
}
void Start()
{
string path = Application.streamingAssetsPath+"\Login.xml";
LoadXml(path ,ref m_Logininfo);
}
}
Unity解析Xml方法二
xml文件类型
<?xml version="1.0"?>
<LoginList>
<Login>
<name>老大</name>
<sex>男</sex>
<id>0</id>
</Login>
<Login>
<name>老二</name>
<sex>女</sex>
<id>1</id>
</Login>
<Login>
<name>老三</name>
<sex>女</sex>
<id>2</id>
</Login>
</LoginList>
解析上文中Xml的解析Demo如下:
public class Demo
{
/// <summary>
/// 用于解析xml的方法
/// </typeparam>
/// <param name="path">xml文件的路径</param>
public void LoadXml(string path)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
//获取根节点---此处一定要添加双斜杠
XmlNodeList nodeList = doc.SelectNodes("//Login");
//查看节点的数量
Debug.Log(nodeList.Count);
//遍历输出.
foreach (XmlNode node in nodeList)
{
string name = node.SelectSingleNode("name").InnerText;
string sex = node.SelectSingleNode("sex").InnerText;
string id = node.SelectSingleNode("id").InnerText;
//输出结果
Debug.Log(name + "--" + sex + "--" + id);
}
}
catch(Exception ex)
{
//用于捕捉异常避免解析过程中出现错误导致程序死掉
//将错误打印出来
Debug.LogError(ex.Message);
}
}
}
Unity解析Xml方法三
xml文件类型
<?xml version="1.0"?>
<LoginList>
<Login>
<name>老大</name>
<sex>男</sex>
<id>0</id>
</Login>
</LoginList>
解析上文中Xml的解析Demo如下:
public class Demo
{
/// <summary>
/// 用于解析xml的方法
/// </typeparam>
/// <param name="path">xml文件的路径</param>
public void LoadXml(string path)
{
try
{
//读取路径下的文件
XDocument document = XDocument.Load(path)
//得到根节点内的内容
XElement root = document.Root;
//查找子节点内容
XElement childnode = root.Element("RECORD");
//查找具体内容
if(childnode!=null)
{
string name = childnode.Element("name").Value;
string id = childnode.Element("id").Value;
string sex = childnode.Element("sex").Value;
//输出结果
Debug.Log(name + "--" + sex + "--" + id);
}
}
catch(Exception ex)
{
//用于捕捉异常避免解析过程中出现错误导致程序死掉
//将错误打印出来
Debug.LogError(ex.Message);
}
}
}