.net 为什么“根级别的数据无效。第 1 行,位置 1。” 对于 XML 文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17947238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why "Data at the root level is invalid. Line 1, position 1." for XML Document?
提问by CJ7
I am using a third-party DLL which transmits an XML document over the internet.
我正在使用通过 Internet 传输 XML 文档的第三方 DLL。
Why would the DLL be throwing the following exception?
为什么 DLL 会抛出以下异常?
Data at the root level is invalid. Line 1, position 1. (see below for full exception text.)
根级别的数据无效。第 1 行,位置 1。(有关完整的例外文本,请参见下文。)
Here are the first few lines of the XML Document:
以下是 XML 文档的前几行:
<?xml version="1.0" encoding="utf-8"?> <REQUEST> <HEADER>
<REQUESTID>8a5f6d56-d56d-4b7b-b7bf-afcf89cd970d</REQUESTID>
<MESSAGETYPE>101</MESSAGETYPE>
<MESSAGEVERSION>3.0.2</MESSAGEVERSION>
Exception:
例外:
System.ApplicationException was caught
Message=Unexpected exception.
Source=FooSDK
StackTrace:
at FooSDK.RequestProcessor.Send(String SocketServerAddress, Int32 port)
at Foo.ExecuteRequest(Int32 messageID, IPayload payload, Provider prov)
at Foo.SendOrder(Int32 OrderNo)
InnerException: System.Xml.XmlException
LineNumber=1
LinePosition=1
Message=Data at the root level is invalid. Line 1, position 1.
Source=System.Xml
SourceUri=""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.LoadXml(String xml)
at XYZ.RequestProcessor.GetObjectFromXML(String xmlResult)
at XYZ.RequestProcessor.Send(String SocketServerAddress, Int32 port)
InnerException:
回答by James Brankin
I eventually figured out there was a byte mark exception and removed it using this code:
我最终发现有一个字节标记异常并使用以下代码将其删除:
string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (xml.StartsWith(_byteOrderMarkUtf8))
{
var lastIndexOfUtf8 = _byteOrderMarkUtf8.Length-1;
xml = xml.Remove(0, lastIndexOfUtf8);
}
回答by E-Max
I can give you two advices:
我可以给你两个建议:
- It seems you are using "LoadXml" instead of "Load" method. In some cases, it helps me.
- You have an encoding problem. Could you check the encoding of the XML file and write it?
- 您似乎正在使用“LoadXml”而不是“Load”方法。在某些情况下,它对我有帮助。
- 你有编码问题。你能检查一下XML文件的编码并写出来吗?
回答by Tadej
Remove everything before <?xml version="1.0" encoding="utf-8"?>
删除之前的所有内容 <?xml version="1.0" encoding="utf-8"?>
Sometimes, there is some "invisible" (not visible in all text editors). Some programs add this.
有时,有一些“不可见”(并非在所有文本编辑器中都可见)。一些程序添加了这个。
It's called BOM, you can read more about it here: https://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding
它称为 BOM,您可以在此处阅读更多相关信息:https: //en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding
回答by Adam Hey
if you are using XDocument.Load(url);to fetch xml from another domain, it's possible that the host will reject the request and return and unexpected (non-xml) result, which results in the above XmlException
如果您使用XDocument.Load(url);从另一个域获取 xml,则主机可能会拒绝请求并返回意外(非 xml)结果,从而导致上述 XmlException
See my solution to this eventuality here: XDocument.Load(feedUrl) returns "Data at the root level is invalid. Line 1, position 1."
请在此处查看我对此事件的解决方案: XDocument.Load(feedUrl) 返回“根级别的数据无效。第 1 行,位置 1。”

