Unity下的 C# 解析XML,读取部分
xml解析类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
public class XmlReaderHelper
{
private XmlDocument xDoc = null;
private XmlNode m_pNode = null;
public void LoadXml(string strData)
{
xDoc = new XmlDocument();
xDoc.LoadXml(strData);
}
public bool SelectRoot(string strNodeName)
{
m_pNode = xDoc.SelectSingleNode(strNodeName);
if (m_pNode == null)
{
Debug.LogWarning("XmlReaderHelper SelectRoot, XmlNode can not be null. key : " + strNodeName);
return false;
}
return true;
}
/// <summary>
/// 选中第一个名为*的节点;
/// </summary>
public bool SelectHead(string strNodeName)
{
if (m_pNode == null)
{
Debug.LogWarning("XmlReaderHelper SelectHead, XmlNode can not be null.");
return false;
}
return ReplaceNode(m_pNode.SelectSingleNode(strNodeName));
}
/// <summary>
/// 选中下一个名为 Head节点名字的 节点;
/// </summary>
public bool SelectNext()
{
if (m_pNode == null)
{
Debug.LogWarning("XmlReaderHelper SelectNext, XmlNode can not be null.");
return false;
}
return ReplaceNode(m_pNode.NextSibling);
}
/// <summary>
/// 返回上一层节点;
/// </summary>
public bool ReturnParent()
{
if (m_pNode == null)
{
Debug.LogWarning("XmlReaderHelper ReturnParent, XmlNode can not be null.");
return false;
}
return ReplaceNode(m_pNode.ParentNode);
}
/// <summary>
/// Check 并且 替换 pNode;
/// </summary>
private bool ReplaceNode(XmlNode targetNode)
{
if (targetNode == null)
{
return false;
}
m_pNode = targetNode;
return true;
}
/// <summary>
/// 读取节点内容;
/// </summary>
public string ReadNode()
{
if (m_pNode == null)
{
Debug.LogWarning("XmlReaderHelper ReadNode, XmlNode can not be null.");
return null;
}
return m_pNode.InnerText;
}
public string ReadAttribute(string strKey)
{
if (m_pNode == null)
{
Debug.LogWarning("XmlReaderHelper ReadNode, XmlNode can not be null.");
return null;
}
XmlAttributeCollection xac = m_pNode.Attributes;
if (xac.Count <= 0)
{
Debug.LogWarning("XmlReaderHelper ReadNode, XmlAttributeCollection can not be null.");
return null;
}
XmlAttribute xa = xac[strKey];
if (xa == null)
{
Debug.LogWarning("XmlReaderHelper ReadNode, XmlAttribute can not be null. key : " + strKey);
return null;
}
return xa.Value;
}
public void Close()
{
xDoc = null;
m_pNode = null;
}
}
解析实际使用代码
public void Parse(string strData)
{
XmlReaderHelper xrh = new XmlReaderHelper();
xrh.LoadXml(strData);
if (!xrh.SelectRoot("root"))
{
Debug.LogError("GuideData Parse, root Node Can Not Be Null.");
return;
}
if (xrh.SelectHead("pack"))
{
while (true) // Pack节点
{
//-----------------------------------------------------
if (xrh.SelectHead("c"))
{
while (true) // Condition节点
{
if (xrh.SelectHead("i"))
{
while (true) // Condition Item节点
{
string strKey = xrh.ReadAttribute("key");
string strValue = xrh.ReadNode();
Debug.Log("ci k " + strKey);
Debug.Log("ci v " + strValue);
if (!xrh.SelectNext())
{
xrh.ReturnParent();
break;
}
}
}
if (!xrh.SelectNext())
{
xrh.ReturnParent();
break;
}
}
}
//-----------------------------------------------------
if (xrh.SelectHead("v"))
{
while (true) // ViewEffect节点
{
if (xrh.SelectHead("i"))
{
while (true) // ViewEffect节点 Item节点
{
string strKey = xrh.ReadAttribute("key");
string strValue = xrh.ReadAttribute("value");
Debug.Log("vi k " + strKey);
Debug.Log("vi v " + strValue);
if (!xrh.SelectNext())
{
xrh.ReturnParent();
break;
}
}
}
if (!xrh.SelectNext())
{
xrh.ReturnParent();
break;
}
}
}
//-----------------------------------------------------
if (!xrh.SelectNext())
{
xrh.ReturnParent();
break;
}
}
}
}
xml内容
<pack>
<c>
<i key="1">11001</i>
</c>
<v>
<i key="1" value="20,900,200,100"/>
<i key="2" value="测试文字\n测试文字"/>
<i key="3" value="270,20,900"/>
</v>
</pack>
</root>