关于xml读取

  1. Unity中所有的文本类型的资源对应的类型为TextAsset,所以除了使用Resources.Load加载以外还可以拖拽赋值
  2. 一楼使用url加载的方式读取Resources文件夹下的xml,这样的方式并不推荐使用
    这样的方式仅在编辑器模式下可以,发布后Resources文件所有的资源都会被压缩,这时候用Url加载一定找不到文件
    即使要使用Url加载也是把Xml文件放在StreamingAssets文件夹下
  3. doc.LoadXml 里面的参数是文件路径,不是文件内容
  4. 从你的截图来看 你的XML没有被Unity识别,因为Xml文件的图标并不是文本资源的图标,
    所以表现的特征有 1. 这xml应该不能拖拽 2. Resources.Load ("Xml4") 返回值应该为空
    你第二张的截图空引用异常 也印证了这点
    你可以尝试重新加载Xml 来解决未被识别的问题,就是 右键点击xml文件 --》Reimport
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
public class XmlDataUtility
{

    string m_file;
    Stream m_fs;
    public BinaryReader br
    {
        protected set;
        get;
    }

    public BinaryWriter bw
    {
        protected set;
        get;
    }


    public XmlDataUtility()
    {
    }

    public BinaryReader BeginReaderBinary(string strFile)
    {
        m_file = strFile;

        string path = "";
#if UNITY_EDITOR

        if (Application.isPlaying) {
            path += Util.DataPath;
        }
        else {
            string target = string.Empty;
            if (Application.platform == RuntimePlatform.OSXEditor){
                target = "Mac";
            }
            else {
                target = "Windows32";
            }

            path = Application.dataPath + "/StreamingAssets/" + target + "/";

        }
        path += "table/";

#else
        path += Util.DataPath;
        path += "table/";
#endif


        string strFilePath = path + strFile;
        //Resources


        byte[] bytes = WebBytes.GetBytes(strFilePath);
        m_fs = new MemoryStream(bytes);
        br = new BinaryReader(m_fs);

        return br;
    }


    public XmlDocument BeginWriteBinary(string root, string strFile)
    {
        m_file = strFile;

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(PathSetting.platXmlPath() + strFile + ".xml");

        //生成的目录(目前只需要windoweditor)
        string path = root + "table/";
        string strFilePath = path + strFile + ".bin";
        if (File.Exists(strFilePath))
        {
            File.Delete(strFilePath);
        }

        m_fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write, FileShare.Write);
        bw = new BinaryWriter(m_fs);

        return xmlDoc;

    }

    ////TODO:测试中
    //public XmlDocument BeginMSOWriteBinary(string root, string strFile, string sheetName, ref XmlNodeList rows)
    //{
    //    XmlDocument xmlDoc = BeginWriteBinary(root, strFile);

    //    GetMSORows( xmlDoc, string sheetName)


    //    XmlNodeList workSheet = xmlDoc.GetElementsByTagName("Worksheet");
    //    if (workSheet.Count == 0)
    //    {
    //        return xmlDoc;
    //    }

    //    XmlNodeList tables = null;
    //    foreach (XmlElement sheet in workSheet)
    //    {
    //        string name = sheet.GetAttribute("ss:Name");
    //        if (name == sheetName)//"common"
    //        {
    //            tables = sheet.GetElementsByTagName("Table");
    //        }
    //    }
    //    if (tables == null || tables.Count <= 0)
    //    {
    //        return xmlDoc;
    //    }

    //    foreach (XmlElement row in tables)
    //    {
    //        rows = row.GetElementsByTagName("Row");
    //        if (rows.Count > 0)
    //            break;
    //    }

    //    return xmlDoc;
    //}

    public static XmlNodeList GetMSORows(XmlDocument xmlDoc, string sheetName)
    {
        XmlNodeList workSheet = xmlDoc.GetElementsByTagName("Worksheet");
        if (workSheet.Count == 0)
        {
            return null;
        }

        XmlNodeList tables = null;
        foreach (XmlElement sheet in workSheet)
        {
            string name = sheet.GetAttribute("ss:Name");
            if (name == sheetName)//"common"
            {
                tables = sheet.GetElementsByTagName("Table");
            }
        }
        if (tables == null || tables.Count <= 0)
        {
            return null;
        }

        XmlNodeList rows = null;
        foreach (XmlElement row in tables)
        {
            rows = row.GetElementsByTagName("Row");
            if (rows.Count > 0)
                break;
        }

        return rows;
    }


    public void End()
    {
        if (m_fs != null)
        {
            m_fs.Close();
        }
        if (br != null)
        {
            br.Close();

            //Debug.Log("ReaderBinary: " + m_file);

        }
        if (bw != null)
        {
            bw.Close();

            //Debug.Log("WriteBinary: " + m_file);
        }
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,179评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,328评论 19 139
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,672评论 7 249
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • 明天就要高考了,朕来晒一下朕高三时候的书单。 网络小说《盛唐风月》《九棺》《庆余年》《酒神》,不怎么偏重言情。还有...
    零琋阅读 1,852评论 23 6