为什么"数据在根级别无效。 1号线,位置1"对XML文档? [英] Why "Data at the root level is invalid. Line 1, position 1." for XML Document?

查看:738
本文介绍了为什么"数据在根级别无效。 1号线,位置1"对XML文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是第三方DLL文件,传输通过互联网的XML文档。

为什么该DLL被抛出下面的异常?

  

在根级别的数据无效。 1号线,位置1(见下文   全异常文本。)

下面是XML文档的前几行:

 < XML版本=1.0编码=UTF-8&GT?; <请求> <报头GT;
    <请求ID> 8a5f6d56-d56d-4b7b-b7bf-afcf89cd970d< /请求ID>
    <消息类型> 101< /为messageType>
    < MESSAGEVERSION> 3.0.2< / MESSAGEVERSION>
 

异常:

  System.ApplicationException被抓
      消息=意外的异常。
      来源= FooSDK
      堆栈跟踪:
           在FooSDK.RequestProcessor.Send(字符串SocketServerAddress,的Int32端口)
           在Foo.ExecuteRequest(的Int32 MESSAGEID,IPayload有效载荷,提供省)
           在Foo.SendOrder(的Int32 OrderNo)
      的InnerException信息:System.Xml.XmlException
           行号= 1
           LinePosition = 1
           消息=数据在根级别无效。行1,位置1。
           来源=的System.Xml
           SourceUri =
           堆栈跟踪:
                在System.Xml.XmlTextReaderImpl.Throw(例外五)
                在System.Xml.XmlTextReaderImpl.Throw(字符串资源,字符串ARG)
                在System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
                在System.Xml.XmlTextReaderImpl.ParseDocumentContent()
                在System.Xml.XmlTextReaderImpl.Read()
                在System.Xml.XmlLoader.Load(XmlDocument的文档,XmlReader的读者,布尔preserveWhitespace)
                在System.Xml.XmlDocument.Load(XmlReader的读者)
                在System.Xml.XmlDocument.LoadXml(XML字符串)
                在XYZ.RequestProcessor.GetObjectFromXML(字符串xmlResult)
                在XYZ.RequestProcessor.Send(字符串SocketServerAddress,的Int32端口)
           的InnerException:
 

解决方案

我终于想通了,有一个字节的标志异常,并使用该code删除它:

 字符串_byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.Get preamble());
如果(xml.StartsWith(_byteOrderMarkUtf8))
{
    XML = xml.Remove(0,_byteOrderMarkUtf8.Length);
}
 

I am using a third-party DLL which transmits an XML document over the internet.

Why would the DLL be throwing the following exception?

Data at the root level is invalid. Line 1, position 1. (see below for full exception text.)

Here are the first few lines of the XML Document:

<?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:

解决方案

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))
{
    xml = xml.Remove(0, _byteOrderMarkUtf8.Length);
}

这篇关于为什么&QUOT;数据在根级别无效。 1号线,位置1&QUOT;对XML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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