Unity C#读取xml文件方法整理

本文讲述几种xml的读取方法。

1

第一种格式

文件如下格式

<?xml version="1.0" standalone="yes"?>
<RECORDS>
<RECORD Top="1" Name="一等奖-索尼什么鬼" Max="-27" Min="27"/>
<RECORD Top="2" Name="二等奖-Oppc" Max="86" Min="147"/>
</RECORDS>

我们可以这么读。如下:

private string path1 = "needle";
 private string path2 = "dish";
/// <summary> 读取xml </summary>
    Dictionary<int, AwardsData> LoadXml(int type)
    {
        string name = (type == 1) ? path1 : path2;
        string path = Application.dataPath + "/Resources/Xml/" + name + ".xml";
        XmlReader reader = new XmlTextReader(path);

        Dictionary<int, AwardsData> dic = new Dictionary<int, AwardsData>();

        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element)
            {
                if (reader.LocalName == "RECORD")
                {
                    AwardsData data = new AwardsData();
                    for (int i = 0; i < reader.AttributeCount; i++)
                    {
                        reader.MoveToAttribute(i);
                        if (reader.Name == "Top") data.top = int.Parse(reader.Value);
                        else if (reader.Name == "Name") data.name = reader.Value;
                        else if (reader.Name == "Max") data.max = float.Parse(reader.Value);
                        else if (reader.Name == "Min") data.min = float.Parse(reader.Value);
                    }
                    if (!dic.ContainsKey(data.top)) dic.Add(data.top, data);
                }
            }
        }
        return dic;
    }

这种格式我们就以这样的方式就能读出。

第二种格式[树结构]

如下

<?xml version="1.0" encoding="utf-8"?>
<root>
 <row>
 <id>1</id>
 <name>普通卡</name>
 </row>
 <row>
 <id>2</id>
 <name>高级</name>
 </row>
</root>

方法如下:

public Dictionary<int, CardBagDataList> GetLuckyCardList()
{
	GetXmlDoc();
	Dictionary<int, CardBagDataList> cardHomeDic = new Dictionary<int, CardBagDataList>();
	XmlElement rootElem = xml.DocumentElement;
	XmlNodeList rowList = rootElem.GetElementsByTagName("row");
	foreach (XmlNode xn in rowList)
	{
		CardBagDataList chd = new CardBagDataList();
		chd.ID = int.Parse(xn["id"].InnerText);
		chd.Name = xn["name"].InnerText;

		if (!cardHomeDic.ContainsKey(chd.ID))
		{
			cardHomeDic.Add(chd.ID, chd);
		}
	}
	return cardHomeDic;
}

 第三种格式

格式如下:

<Map Name="大战草泥马" Id="6102" GridX="50" GridY="50">
 <ObstacleLayer>
 <Obstacle GirdId="180000" />
 <Obstacle GirdId="170000" />
 <Obstacle GirdId="50013" />
 </ObstacleLayer>
 <MonsterLayer>
 <Monster Name="大戟士" ImageId="40378"/>
 <Monster Name="投石车" ImageId="40380" />
 </MonsterLayer>
</Map>

和上一种比较像。结合第一种第二种。

方法如下:

string path = Application.dataPath + "/Resources/Xml/" + name + ".xml";
XmlReader reader = new XmlTextReader(path);
while (reader.Read())
{
	if (reader.NodeType == XmlNodeType.Element)
	{
		if (reader.LocalName == "Obstacle")
		{
			for (int i = 0; i < reader.AttributeCount; i++)
			{
				reader.MoveToAttribute(i);
				if (reader.Name == "GirdId")
				{
					int grid = int.Parse(reader.Value);
					mObstacleList.Add(grid);
				}
			}
		}
		else if (reader.LocalName == "Monster")
		{
			MonsterModeMap data = new MonsterModeMap();
			for (int i = 0; i < reader.AttributeCount; i++)
			{
				reader.MoveToAttribute(i);
				if (reader.Name == "GridId")
				{
					data.grid = int.Parse(reader.Value);
				}
				else if (reader.Name == "Id")
				{
					data.id = int.Parse(reader.Value);
				}
			}
			if (!mBattlefieldMapMonsterModeInfoDic.ContainsKey(data.id))
			{
				mBattlefieldMapMonsterModeInfoDic.Add(data.id, data);
			}
		}
		else if (reader.LocalName == "Map")
		{
			for (int i = 0; i < reader.AttributeCount; i++)
			{
				reader.MoveToAttribute(i);
				if (reader.Name == "Id")
				{
					mMapId = int.Parse(reader.Value);
				}
				else if (reader.Name == "GridX")
				{
					mGridX = int.Parse(reader.Value);
				}
			}
		}
	}
}

 

本文链接:

https://www.bobsong.net/489.html
1 + 2 =
快来做第一个评论的人吧~