//有时会遇到xml格式时UTF—8存在BOM的时候,用UE另存去掉BOM就OK了
什么是BOM呢?
BOM:Byte Order Mark,中文名字节顺序标记。UCS规范建议在传输字节流前,先传输BOM来判断字节顺序。其实UTF-8是不需要用BOM来表明字节顺序的,但是可以用BOM来表明编码方式。BOM的UTF-8编码是EF BB BF,所以呢,如果接受者收到EF BB BF开头的字节流,就说明它是UTF-8编码了。由此可见,对于UTF-8来说,BOM是可有可无的,可是,有的XML解析方式不认这个BOM,所以就报错了。
这里写代码片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Xml.Linq;
using System.IO;
public class LoadXML : MonoBehaviour {
string xmlPath ;
void Start () {
if (File.Exists(Application.streamingAssetsPath + "/test.xml"))
{
Debug.Log("True");
}
byte[] xmlDocByte= File.ReadAllBytes(Application.streamingAssetsPath+"/test.xml");
string xmlStr = System.Text.Encoding.UTF8.GetString(xmlDocByte);
Debug.Log(xmlStr);
//有时会遇到xml格式时UTF—8存在BOM的时候,用UE另存去掉BOM就OK了
XDocument xDocument = XDocument.Parse(xmlStr);
//Descendants获取根节点下全部节点XElement[]
XElement[] array = xDocument.Element("Root").Descendants("ResourceData").ToArray();
foreach (var item in array)
{
if (item.Element("Key").Value== "fgcc")
{
//Element 节点,里面可以写属性Attributes
//var s= item.Element("EffectObjs").Element("Prefab").Element("Transfrom").Attributes().ToList();
var s = item.Element("EffectObjs").Element("Prefab").Element("Transfrom").Attribute("Position").Value;
Debug.Log(s);
}
}
}
}