本文共 5613 字,大约阅读时间需要 18 分钟。
01.创建XML文件
先锋书店 Everday MrsTang 2020 500 快乐书店 Everday MrsZhang 1984 600
02.解析
using System;using System.Collections.Generic;using System.IO;using System.Xml;namespace XMl{ class Program { static void Main(string[] args) { Listbooks =ParseXmol(@"C:\Users\911512\source\repos\XMl\XMl\MyXml.xml"); for (int i = 0; i < books.Count; i++) { Console.WriteLine(books[i].Lang); } Console.ReadKey(); } static List ParseXmol(string path) { List list = null; try { list=new List (); XmlDocument xml=new XmlDocument(); //验证XML文档时经常用到的一个类 XmlReaderSettings settings =new XmlReaderSettings(); //是否忽略注释。 settings.IgnoreComments = true; if (!File.Exists(path)) { Console.WriteLine("路径不存在:"+path); } //为指定路径的文件创建 StreamReader 类的实例 从流中读取字符串 using (StreamReader reader=new StreamReader(path)) { //读取来自流的当前位置到结尾的所有字符 string books=reader.ReadToEnd(); //从指定的字符串加载 XML 文档 //字符串的格式已经为xml类型,用loadxml进行解析 xml.LoadXml(books); //获取根节点 XmlNode rooNode =xml.SelectSingleNode("root"); //获取根节点下所有的的子节点 XmlNodeList nodeList=rooNode.ChildNodes; foreach (XmlNode node in nodeList) { Book book=new Book(); int id = int.Parse(node.Attributes[1].InnerText); string catagory = node.Attributes[0].InnerText; string name = node["name"].InnerText; string title = node["title"].InnerText; //将节点转换为元素 以便于获取属性值 XmlElement element = (XmlElement)node["title"]; string lang = element.GetAttribute("lang"); int year = int.Parse(node["year"].InnerText); int price= int.Parse(node["price"].InnerText); book.Id = id; book.Catagory = catagory; book.Name = name; book.Title = title; book.Lang = lang; book.Year = year; book.Price = price; list.Add(book); } } } catch (Exception e) { Console.WriteLine(e); } return list; } } public class Book { public int Id { get; set;} public string Catagory { get; set; } public string Name { get; set; } public string Title { get; set; } public string Lang { get; set; } public int Year { get; set; } public int Price { get; set; } }}
03.unity 解析XMl
创建模板添加映射
04. 导出xml10 1 10 55
20 2 100 66
05解析
using System;using System.Collections;using System.Collections.Generic;using System.IO;using System.Xml;using UnityEngine;public class XmlManager : MonoBehaviour{ void Start() { Listlist = ParseXmol("Map1"); for (int i = 0; i < list.Count; i++) { Debug.Log(list[i].Id); } } private List ParseXmol(string path) { List list=new List (); try { XmlDocument xml = new XmlDocument(); //验证XML文档时经常用到的一个类 XmlReaderSettings settings = new XmlReaderSettings(); //是否忽略注释。 settings.IgnoreComments = true; TextAsset str = Resources.Load (path); Debug.Log(str.text); //从指定的字符串加载 XML 文档 //字符串的格式已经为xml类型,用loadxml进行解析 xml.LoadXml(str.text); //获取根节点 XmlNode rooNode = xml.SelectSingleNode("ROOT"); //获取根节点下所有的的子节点 XmlNodeList nodeList = rooNode.ChildNodes; foreach (XmlNode node in nodeList) { MapInfo map = new MapInfo(); map.Id = int.Parse(node.Attributes["id"].InnerText); map.Wait = int.Parse(node["wait"].InnerText); map.EnmeyName = node["enemyname"].InnerText; map.Level = int.Parse(node["level"].InnerText); map.Wave = int.Parse(node["wave"].InnerText); list.Add(map); } } catch (Exception e) { Debug.Log(e.Message); } return list; }} class MapInfo{ public int Id { get; set; } public int Level { get; set; } public int Wave { get; set; } public string EnmeyName { get; set; } public int Wait{ get; set; }}
转载地址:http://ryrxo.baihongyu.com/