开发者

Send XML file error 'Data at the root level is invalid. Line 1, position 39'

I am attempting to send my XML file through an API. I have done this no problem by using the below code, however when I try and send the FILE over it will not work, I am now getting in the browser:

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

Without trying to send FILE it works:

' 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)

But trying to send FILE it does NOT work, here is FULL code. The file is replicated from the code above so I know the file is good:

    <%@ Page Title="MAIN" Language="vb" Explicit="true" AspCompat="true" %>
   开发者_如何学C <% 
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        
%>

UPDATE I have updated the above code from the response below. I am now getting in the browser:

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

Here is the XML I am sending as a test:

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


I believe that you need to send the content of the document, not the XmlDocument object (as this class is not serializable). The following line should do the trick using an XDocument:

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

If using an XmlDocument the following code will work:

Dim doc As XmlDocument

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

oXMLHttp.Send(doc.OuterXml)


If your code above your not sending the XML but a string that reads "sample.xml"

oXMLHttp.Send("sample.xml")

Should you be sending the doc object?

oXMLHttp.Send(doc)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜