根级别的数据无效。第1行1。 [英] data at the root level is invalid. line 1 position 1.

查看:66
本文介绍了根级别的数据无效。第1行1。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我需要帮助。这是代码。



Hello. I need help. Here is the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String str = "https://mx.fortdigital.net/http/send-message?username=test&password=test&to=6593365219&from=FortDigital&message-type=sms.automatic&message=Message+Testing+Haloo";

            string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
            if (str.StartsWith(_byteOrderMarkUtf8))
            {
                str = str.Remove(0, _byteOrderMarkUtf8.Length);
            }

            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.Load(str);
        }

    }
}





我收到此错误。



根级别的数据无效。第1行,第1位。



我试过没有StartsWith检查。还是一样的错误。



有什么建议吗?谢谢。



I get this error.

Data at the root level is invalid. line 1, position 1.

I've tried without the "StartsWith" check. Still same error.

Any advice? Thank you.

推荐答案

如果您尝试从该URL读取XML,则需要将链接指向xml文档,而不是
If you are trying to read the XML from that URL then you will need to have the link point to an xml document and not
OK: test_397_346<br>

这是无效的XML。您可以在以下网址 [ ^ ]使用XmlTextReader而不是XmlDocument



但是,如果你是试图将 str 解释为xml然后它无效 - 尝试将&符号& 更改为&安培; amp; (删除空格 - 仅包括它们以阻止此编辑器将其更改为&)

您还需要文件的根节点,因此请尝试使用标签

例如

which is not valid XML. You can find more information on reading XML from a URL here[^] which uses XmlTextReader instead of XmlDocument

If however, you are trying to interpret str as xml then it is not valid - try changing the ampersands & to & amp ; (remove the spaces - only included them to stop this editor changing it to &)
You also need a root node for the file so try surrounding the text with tags
e.g.

String str = "<url>https://mx.fortdigital.net/http/send-message?username=test&amp;password=test&amp;to=6593365219&amp;from=FortDigital&amp;message-type=sms.automatic&amp;message=Message+Testing+Haloo</url>";



您正在使用加载将文件名作为参数的方法,因此它试图将该文本作为文件打开 - 改为使用 LoadXml


You are using the Load method that takes a filename as a parameter so it is trying to open that text as a file - use LoadXml instead.


这篇关于根级别的数据无效。第1行1。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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