none
XML document must have a top level element

    Question

  • Hi,

    First time I try to send XML data over HTTP from vbscript to ASP, and...it doesn't work!

    If I load an XML from my asp page, I can parse data normally.

    If I send the same xml from my vbscript ASp gives me an error "XML document must have a top level element" at line 0

    '---------------------------------------
    strxml = "c:\bri\note.xml"

    xmltext = "<ROOT>"
    xmltext = xmltext & "<note>"
    xmltext = xmltext & "<magazine>Test</magazine>"
    xmltext = xmltext & "</note>"
    xmltext = xmltext & "</ROOT>"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(strxml,2,True)
    objTextFile.WriteLine(xmltext)
    objTextFile.Close

    Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
    xmlhttp.open "POST", "http://server/mdlsfix/receive.asp",False
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

    Set xmldom = CreateObject("Microsoft.XMLDOM")

    xmlDom.load strxml
    xmlhttp.send xmldom
    MsgBox xmlhttp.responseText

    '---------------------------------------

    When I run this reponsetext gives me the entire content of my XML (including tags)

    My asp looks like this :
    <%
      set xmldom = Server.CreateObject("Microsoft.XMLDOM")
      xmldom.Load(Request)

    If xmldom.parseError.errorCode <> 0 Then
      Set myErr = xmldom.parseError
      Response.Write   myErr.reason
      Response.Write xmlDom.parseError.line
    else
      strText = "You sent me: " & xmldom.xml
      Response.Write xmldom.xml
    end if

    %>

    Result : XML document must have a top level element. 0

    If I load the xml file directly instead of "request" I get "you sent me : Test"

    Could you please advise me on this. I've been browsing internet for days trying different exemples, without success.

    using xmlDom.loadXML strxml vbscript returns msxml3.dll: System error: -1072896682 (XML document must have a top level element)

     

    Thanks in advance !!!

    briac

     

     

    Tuesday, January 25, 2011 2:34 PM

All replies

  • You'll need to set content type to text/xml on server side.
    Thursday, January 27, 2011 2:00 AM
    Moderator
  • Thanks for your reply, unfortunately this is something I have already tried.

    Adding this line, the page tries to load the XML but I still returns

    XML document must have a top level element.
    

    On this same forum I read a comment from someone else :

    I think the most common cause of "Line 0 Pos 0: XML document must have a top level element." error is trying to load Xml from an empty file. The other scenario I came across was trying to read from the same stream twice without moving the stream pointer to the beginning (after reading for the first time the stream pointer was at the very end of the xml document - moving the pointer to the begining before any subsequent reading fixed the problem).

    How do we achieve this in asp ?

    I also tried using MSXML2.DOMDocument.3.0 but it doesn't make any difference. I can load and display the xml file when it's loaded from a local path but when it's posted from HTTPREQUEST, request is well sent back but  I receive XML document must have a top level element on the server side. I don't understand this

    Help would be much appreciated ...

    Thursday, January 27, 2011 7:26 AM
  • Have you at least followed the advice already given, namely to use loadXML, not load in your client-code if you have a string with XML markup and to setRequestHeader("Content-Type", "text/xml")?
    MVP Data Platform Development My blog
    Thursday, January 27, 2011 1:18 PM
  •  

    I tried LoadXML on the client,

    but if like you suggested I don't load on the client and I set loadXML(request) on the server I get a "Type mismatch :xmldom.LoadXML"

    [code]

    xmltext = "<ROOT>"
    xmltext = xmltext & "<note>"
    xmltext = xmltext & "<magazine>Test</magazine>"
    xmltext = xmltext & "</note>"
    xmltext = xmltext & "</ROOT>"

    dim xmlhttp
    set xmlhttp = Createobject("msxml2.ServerXMLHTTP.3.0")

    xmlhttp.Open "POST","http://server/mdlsfix/receive.asp",false

       'xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
       'xmlhttp.setRequestHeader "Content-Type", "application/xml"
    xmlhttp.setRequestHeader "Content-Type", "text/xml"

    xmlhttp.send xmltext

    wscript.echo xmlhttp.responseText
    Set xmlhttp = nothing

    'ASp code

    <%
      set xmldom = Server.CreateObject("MSXML2.DOMDocument.3.0")

      xmldom.async = False
      xmldom.LoadXML(request)
     
      If xmldom.parseError.errorCode <> 0 Then
        Set myErr = xmldom.parseError
        Response.Write  myErr.reason
        Response.Write xmldom.parseError.line
      else
        Response.Write(  "<XMP>" & xmldom.xml & "</XMP>" )
      End If

    %>

    [code]

    I really don't want to give up on this, but I don't where to look at anymore...
    I also tried with xmldom.Load(Request.BinaryRead(Request.TotalBytes))

    every time, the client gets totalbytes' value whereas the server says totalbyte = 0

    Thursday, January 27, 2011 2:45 PM
  • On the server you need to use the code you had

      xmldom.load Request

    there the load method is the correct approach.

    On the client where you construct the XML from a string you don't need to write to a file, instead you can do

    xmltext = "<ROOT>"
    xmltext = xmltext & "<note>"
    xmltext = xmltext & "<magazine>Test</magazine>"
    xmltext = xmltext & "</note>"
    xmltext = xmltext & "</ROOT>"


    Set xmldom = CreateObject("Msxml2.DOMDocument.3.0")

    xmldoc.loadXML xmltext

    then pass xmldoc to the send method of an Msxml2.XMLHTTP.3.0 object.

     

     


    MVP Data Platform Development My blog
    Thursday, January 27, 2011 2:53 PM
  • Now I feel I should stop scripting for the rest of my life...but really, I could use this piece of code!

    I still get the same issue. My code as it is :

    [code]

    xmltext = "<ROOT>"
    xmltext = xmltext & "<note>"
    xmltext = xmltext & "<magazine>Test</magazine>"
    xmltext = xmltext & "</note>"
    xmltext = xmltext & "</ROOT>"


    Set xmldoc = CreateObject("Msxml2.DOMDocument.3.0")
    xmldoc.loadXML xmltext

    Set xmlhttp = CreateObject("Msxml2.XMLHTTP.3.0")
    xmlhttp.Open "POST","http://server/mdlsfix/receive.asp",false

       'xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
       'xmlhttp.setRequestHeader "Content-Type", "application/xml"
    xmlhttp.setRequestHeader "Content-Type", "text/xml"


    If xmlDoc.parseError.errorCode Then
        wscript.Echo "parse error"
    Else
        xmlhttp.send xmldoc
         wscript.echo xmlhttp.responseText
    End If


    Set xmlhttp = nothing

    ASp page :

    <%
      set xmldom = Server.CreateObject("MSXML2.DOMDocument.3.0")
      response.ContentType = "text/xml"

      xmldom.async = False
      xmldom.Load request
     
      If xmldom.parseError.errorCode <> 0 Then
        Set myErr = xmldom.parseError
        Response.Write  myErr.reason
        Response.Write xmldom.parseError.line
      else
        Response.Write(  "<XMP>" & xmldom.xml & "</XMP>" )
      End If

    %>
    [code]

    I wouldn't be mad at you if you give up on me....

    Thursday, January 27, 2011 3:12 PM
  • I copied the samples from your latest post, I put the first sample into a .vbs file, the second into a .asp file, adapted the files names so I have

    xmltext = "<ROOT>"
    xmltext = xmltext & "<note>"
    xmltext = xmltext & "<magazine>Test</magazine>"
    xmltext = xmltext & "</note>"
    xmltext = xmltext & "</ROOT>"
    
    
    Set xmldoc = CreateObject("Msxml2.DOMDocument.3.0")
    xmldoc.loadXML xmltext
    
    Set xmlhttp = CreateObject("Msxml2.XMLHTTP.3.0")
    xmlhttp.Open "POST","http://localhost/javascript/test2011012701.asp",false
    
      'xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
      'xmlhttp.setRequestHeader "Content-Type", "application/xml"
    xmlhttp.setRequestHeader "Content-Type", "text/xml"
    
    
    If xmlDoc.parseError.errorCode Then
      wscript.Echo "parse error"
    Else
      xmlhttp.send xmldoc
       wscript.echo xmlhttp.responseText
    End If
    
    
    Set xmlhttp = nothing
    

    and

    <%@ Language="VBScript" %>
    <%
     set xmldom = Server.CreateObject("MSXML2.DOMDocument.3.0")
    
     xmldom.async = False
     xmldom.Load request
     
     If xmldom.parseError.errorCode <> 0 Then
      Set myErr = xmldom.parseError
      Response.Write myErr.reason
      Response.Write xmldom.parseError.line
     else
      Response.Write( "<XMP>" & xmldom.xml & "</XMP>" )
     End If
    
    %>
    

    then I run the .vbs file with cscript file.vbs and it does not show any errors, instead it outputs

    <XMP><ROOT><note><magazine>Test</magazine></note></ROOT>
    </XMP>
    
    So based on that the code should work, I am not sure why you still get an error. Please compare the code samples and check if there are any differences.

     

     

     

     

     


    MVP Data Platform Development My blog
    Thursday, January 27, 2011 3:35 PM
  • First thank you very much for taking the time to test this.

    I thought I said that in my previous post but what I am trying to achieve is to display this XML on the server.
    So far I always received the response back from the server without being able to display them on the server.

    basically I am trying to XML send data from a vbscript to a webserver. The webserver will manipulate them and display the result in the browser (it's even better if there is a response back to the client, but that is not the goal here)

    Really sorry if I didn't make myself clear earlier. I don't mean to waste anybody's time.

     

    briac

    Thursday, January 27, 2011 4:12 PM
  • I don't understand what you want to achieve and where the problem is. What does "display this XML on the server" mean? A HTTP web server like IIS running your ASP code does receive a HTTP request and sends a HTTP response back. It can also as part of the ASP code processing create or manipulate files on the server. But during the processing of a HTTP request the server does not display anything, the client sending the request and receiving the response might display the response.

    So what is your client-side code, VBScript in a WSH script file or VBScript in a HTML document loaded in IE?


    MVP Data Platform Development My blog
    Thursday, January 27, 2011 4:31 PM
  • I knew my goal was not exactly what this method is made for, but I thought it could serve my needs.

    I am creating an asp website that collect data from WSH scripts scheduled to run on different servers. I need to send those data over HTTP to my web server, XML is a good way to transfer data over HTTP.

    I first tried this method to load an xml file from my local disk and manipulate values to display them in a web page so far so good loading an XML file directly with its filename works,I can see XML values in my reponse.write if I open this page:  http://server/mdlsfix/receive.asp

    then I tried to do the same, but using an HTML post request with vbscript, in that scenario I can't use those data and display them in a web page I can only return the result back to the client. How can I display in a web page the posted values from my vbscript? I am quite familiar with HTML forms but not an expert.

     


    Thursday, January 27, 2011 5:35 PM