【发布时间】: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