Unity下的 C# 解析XML,读取部分

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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值