The data at the root level is invalid. Line 1, position 1.

在尝试使用C#读取XML文件并计算url节点数量时,遇到错误Thedataattherootlevelisinvalid.Line1,position1.。错误出现在加载XML文档的步骤,尽管XML文件本身是正确的。将LoadXml方法替换为Load(file.FullName)后问题依然存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The data at the root level is invalid. Line 1, position 1.

 C#, 读取xml文件中节点数量

       int nodeCount = 0;
                        XmlDocument doc = new XmlDocument();

                        doc.Load(file.FullName);
                        //获得根节点
                        XmlNode rootNode = doc.DocumentElement;
                        //在根节点中寻找节点
                        foreach (XmlNode node in rootNode.ChildNodes)
                        {
                            //找到对应的节点
                            if (node.Name == "url")
                            {
                                nodeCount = nodeCount + 1;
                            }
                        }

今天在读取XML时,突然遇到这问题了,

总是提示: 

The data at the root level is invalid. Line 1, position 1.

-XmlDocument doc = new XmlDocument();
-doc.LoadXml(FilePath);

就是有错误,XML也是对的。
报错就改成:

doc.Load(file.FullName);

platform win32 -- Python 3.10.5, pytest-7.2.0, pluggy-1.2.0 -- C:\Users\yl\AppData\Local\Programs\Python\Python310\python.exe cachedir: .pytest_cache rootdir: E:\Code, configfile: pytest.ini, testpaths: /Code/case/ plugins: allure-pytest-2.12.0, ordering-0.6 collecting ... collected 0 items / 1 error =================================== ERRORS ==================================== _____________________ ERROR collecting case/test_case1.py _____________________ C:\\Users\\yl\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\_pytest\\python.py:618: in _importtestmodule\n mod = import_path(self.path, mode=importmode, root=self.config.rootpath)\nC:\\Users\\yl\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\_pytest\\pathlib.py:533: in import_path\n importlib.import_module(module_name)\nC:\\Users\\yl\\AppData\\Local\\Programs\\Python\\Python310\\lib\\importlib\\__init__.py:126: in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n<frozen importlib._bootstrap>:1050: in _gcd_import\n ???\n<frozen importlib._bootstrap>:1027: in _find_and_load\n ???\n<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked\n ???\n<frozen importlib._bootstrap>:688: in _load_unlocked\n ???\nC:\\Users\\yl\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\_pytest\\assertion\\rewrite.py:159: in exec_module\n source_stat, co = _rewrite_test(fn, self.config)\nC:\\Users\\yl\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\_pytest\\assertion\\rewrite.py:337: in _rewrite_test\n tree = ast.parse(source, filename=strfn)\nC:\\Users\\yl\\AppData\\Local\\Programs\\Python\\Python310\\lib\\ast.py:50: in parse\n return compile(source, filename, mode, flags,\nE File "E:\\Code\\case\\test_case1.py", line 17\nE assert text_va != '\ufffd\xfb\ufffd\ufffd\u047e\ufffd\ufffd\ufffd\ufffd\ufffd', '\u05e2\ufffd\ufffd\u02a7\ufffd\ufffd'\nE ^\nE SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xd3 in position 0: invalid continuation byte =========================== short test summary info =========================== ERROR case/test_case1.py !!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亚丁号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值