XmlException:文本节点不能以这种状态出现. 1号线,位置1 [英] XmlException: Text node cannot appear in this state. Line 1, position 1

查看:44
本文介绍了XmlException:文本节点不能以这种状态出现. 1号线,位置1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在深入探讨这个问题之前,我知道还有另一个问题听起来和我的完全一样.但是,我尝试了该解决方案(使用Notepad ++将xml文件编码为UTF-8(不带BOM)),但它不起作用.

Before I get into the issue, I'm aware there is another question that sounds exactly the same as mine. However, I've tried that solution (using Notepad++ to encode the xml file as UTF-8 (without BOM) ) and it doesn't work.

XmlDocument namesDoc = new XmlDocument();
XmlDocument factionsDoc = new XmlDocument();
namesDoc.LoadXml(Application.persistentDataPath + "/names.xml");
factionsDoc.LoadXml(Application.persistentDataPath + "/factions.xml");

上面是我遇到问题的代码.我不确定是什么问题.

Above is the code I have problems with. I'm not sure what the problem is.

<?xml version="1.0"  encoding="UTF-8"?>
<factions>
        <major id="0">
...

上面是XML文件的一部分(它的开头-names.xml也是相同的,除了它没有'id'属性).这些文件均以UTF-8编码-在最新的notepad ++版本中,没有在没有BOM的情况下以UTF-8编码"的选项afaik UTF-8与没有BOM的UTF-8相同.

Above is a section of the XML file (the start of it - names.xml is also the same except it has no 'id' attribute). The file(s) are both encoded in UTF-8 - in the latest notepad++ version, there is no option of "encode in UTF-8 without BOM" afaik UTF-8 is the same as UTF-8 without BOM.

有人知道原因是什么吗?还是我做错了什么/忘记了什么? :/

Does anyone have any idea what the cause may be? Or am I doing something wrong/forgetting something? :/

推荐答案

您收到一条错误消息,因为.LoadXml()方法需要一个包含XML data 而不是XML位置的字符串参数. XML 文件.如果要加载XML文件,则需要使用.Load()方法,而不是.LoadXml()方法.

You are receiving an error because the .LoadXml() method expects a string argument that contains the XML data, not the location of an XML file. If you want to load an XML file then you need to use the .Load() method, not the .LoadXml() method.

这篇关于XmlException:文本节点不能以这种状态出现. 1号线,位置1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆