【问题标题】:Send XML file error 'Data at the root level is invalid. Line 1, position 39'发送 XML 文件错误“根级别的数据无效。第 1 行,位置 39'
【发布时间】:2011-10-18 07:49:10
【问题描述】:

我正在尝试通过 API 发送我的 XML 文件。我通过使用下面的代码完成了这个没有问题,但是当我尝试发送文件时它不起作用,我现在进入浏览器:

Data at the root level is invalid. Line 1, position 39.

无需尝试发送 FILE 即可:

' create the Xml that the Msxml2.serverXmlHttp object will send to the Webservice
dim Xml_to_Send
Xml_to_Send = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
Xml_to_Send = Xml_to_Send & "<xmldata>"
Xml_to_Send = Xml_to_Send & "     <Products>"
Xml_to_Send = Xml_to_Send & "          <ProductCode>THE-TEST</ProductCode>"
Xml_to_Send = Xml_to_Send & "          <ProductPrice>100.00</ProductPrice>"
Xml_to_Send = Xml_to_Send & "     </Products>"
Xml_to_Send = Xml_to_Send & "</xmldata>"

oXMLHttp.Send(Xml_to_Send)

但是尝试发送文件它不起作用,这是完整的代码。该文件是从上面的代码复制的,所以我知道该文件是好的:

    <%@ Page Title="MAIN" Language="vb" Explicit="true" AspCompat="true" %>
    <% 
Dim doc As XDocument = XDocument.Load("sample.xml")

    ' create the Msxml2.serverXmlHttp object needed to post the Xml to the WebService
    Dim oXMLHttp
    oXMLHttp = Server.CreateObject("Msxml2.serverXmlHttp")
    oXMLHttp.open("POST", "http://www.mysite.com/net/WebService.aspx?Login=mysite@mysite.com&EncryptedPassword=xxxx&Import=Update", False)
    oXMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
    oXMLHttp.setRequestHeader("Content-Action", "xmldata")
    oXMLHttp.setTimeouts(100000, 100000, 600000, 9999999)
    Server.ScriptTimeout = 10800

    ' Send the Xml  
    oXMLHttp.Send(String.Format("{0}\n\r{1}", doc.Declaration.ToString(), doc.ToString()))

    ' Receive the Xml
    Dim Xml_Returned
    Xml_Returned = oXMLHttp.responseText

    ' Validate the Xml
    Dim xmlDoc
    xmlDoc = Server.CreateObject("Msxml2.DOMDocument")
    xmlDoc.loadXML(Xml_Returned)
    If (Len(xmlDoc.text) = 0) Then
        Xml_Returned = ("<BR><B>ERROR in Response xml:<BR>ERROR DETAILS:</B><BR><HR><BR>") & Xml_Returned
    End If

    ' Display the Xml on the browser
    Response.Write(Xml_Returned)

    ' clean up
    Xml_to_Send = Nothing
    oXMLHttp = Nothing
    doc = Nothing
    xmlDoc = Nothing
    Xml_Returned = Nothing        
%>

更新 我已经从下面的响应中更新了上面的代码。我现在进入浏览器:

Data at the root level is invalid. Line 1, position 39.

这是我作为测试发送的 XML:

<xmldata>
  <Products>
    <ProductCode>AMN-ACE14</ProductCode>
    <ProductPrice>3800.00</ProductPrice>
  </Products>
</xmldata>

【问题讨论】:

  • 这是什么语言?在 vb[a] 中,你不能在一行中声明和赋值...
  • 对不起,它在一个使用VB的.aspx页面中
  • 你到底在做什么?为什么要使用 .NET 程序中的 XmlHttpRequest?
  • 我正在尝试将 XML 文件发送到 API 帖子。我找不到如何通过 C#、Batch 或 Javascript 进行操作。这个过程最终将实现自动化,这是我必须使用的唯一采样。

标签: xml vb.net api xmlhttprequest linq-to-xml


【解决方案1】:

我相信您需要发送文档的内容,而不是 XmlDocument 对象(因为此类不可序列化)。以下行应该使用 XDocument 来解决问题:

oXMLHttp.Send(string.Format("{0}\n\r{1}", doc.Declaration.ToString(), doc.ToString()))

如果使用 XmlDocument,以下代码将起作用:

Dim doc As XmlDocument

doc = New XmlDocument()
doc.Load("sample.xml")

oXMLHttp.Send(doc.OuterXml)

【讨论】:

  • 好吧有道理。我相信 VB.NET 虽然不允许我输入它。它给了我'OuterXml' is not a member of 'System.Xml.Linq.XDocument'
  • @ToddN - 抱歉,我错过了您使用的是 XDocument 而不是 XmlDocument。请看我修改后的答案。
  • 啊,谢谢。它不起作用,在 chrome 中它给了我一个错误False Data at the root level is invalid. Line 1, position 39。有没有办法我可以只使用XmlDocument 而不是XDocument,它看起来更容易使用?
  • @ToddN - 您是否将 XDocument 发送的字符串与您的第一个示例代码中的示例字符串进行了比较?您可以将生成的 XML 添加到您的问题中,以便我查看。有关如何使用 XmlDocument,请参阅答案中的代码。
  • 哇,它做到了。我需要把它作为一个“XmlDocument”,非常感谢!
【解决方案2】:

如果您上面的代码不是发送 XML 而是一个读取为“sample.xml”的字符串

oXMLHttp.Send("sample.xml")

您应该发送 doc 对象吗?

oXMLHttp.Send(doc)

【讨论】:

  • 是的,你是对的,我一直在摆弄它,从未将其改回。它现在仍然没有像帖子中提到的那样做任何事情,但感谢您了解这一点!
  • 您是否调试过 'dim doc As XDocument = XDocument.Load("sample.xml") ' 以确保 doc 确实包含 XML?
  • 我相信我已经使用Response.Write(doc)。在我的浏览器中吐出 XML 中的内容..尽管它仍然没有上传它,而只是显示它。
  • 调用 oXMLHttp.send(doc) 后是否填充了 oXMLHttp.responseXML?如果是这样,它读什么?
  • 有趣,结果什么都没有显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 2020-12-19
  • 2011-10-28
  • 1970-01-01
相关资源
最近更新 更多