gpt4 book ai didi

.net - 为什么 "Data at the root level is invalid. Line 1, position 1."用于 XML 文档?

转载 作者:数据小太阳 更新时间:2023-10-29 01:38:32 26 4
gpt4 key购买 nike

我正在使用通过 Internet 传输 XML 文档的第三方 DLL。

为什么 DLL 会抛出以下异常?

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

这是 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>

异常:

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:

最佳答案

我最终发现存在字节标记异常并使用以下代码将其删除:

 string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (xml.StartsWith(_byteOrderMarkUtf8))
{
var lastIndexOfUtf8 = _byteOrderMarkUtf8.Length-1;
xml = xml.Remove(0, lastIndexOfUtf8);
}

关于.net - 为什么 "Data at the root level is invalid. Line 1, position 1."用于 XML 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17947238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com