XML加载解析报错,存在BOM,解决方案

//有时会遇到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);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值