2011-08-02 40 views
2

我試圖通過API發送我的XML文件。發送XML文件錯誤'根級別的數據無效。第1行,第39位'

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

沒有嘗試發送文件時,它的工作原理:我用下面的代碼,但是當我嘗試發送的文件上,它不會工作,我現在越來越在瀏覽器中完成這沒有問題:

' 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/[email protected]&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 我已經更新從下面的響應上面的代碼。現在我得到的瀏覽器:

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

這是我送的測試XML:

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

這是什麼語言以下行應使用一個XDocument做的伎倆?在vb [a]中,你不能在一行中聲明和賦值... –

+0

對不起,它在.aspx頁面中使用VB – ToddN

+0

你在做什麼?爲什麼要使用.NET程序中的XmlHttpRequest? –

回答

1

我相信你需要發送文檔的內容,而不是XmlDocument對象(因爲這個類不是可序列化的)。

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) 
+0

好吧有道理。我相信VB.NET不允許我輸入它。這是給我''OuterXml'不是'System.Xml.Linq.XDocument''的成員 – ToddN

+0

@ToddN - 對不起,我錯過了你正在使用XDocument而不是XmlDocument。請看我的修改答案。 – stevehipwell

+0

啊,謝謝。它不起作用,在鉻它給了我一個錯誤'在根層面的虛假數據是無效的。 1號線,39號位置。有沒有一種方法可以使用'XmlDocument'而不是'XDocument',它看起來更容易使用? – ToddN

0

如果你的代碼上面你不發送XML,但是一個字符串,上面寫着「樣品.xml「

oXMLHttp.Send("sample.xml") 

您是否應該發送doc對象?

oXMLHttp.Send(doc) 
+0

是的你正確的我擺弄它,永遠不會改變它。它現在沒有做任何事情在帖子中提到,但感謝抓住! – ToddN

+0

您是否調試過「dim doc As XDocument = XDocument.Load(」sample.xml「) '以確保doc包含XML? – Phil

+0

我相信我通過使用'Response.Write(doc)'。它在我的瀏覽器中吐出了XML中的內容。儘管它仍然沒有上傳它,而只是顯示它。 – ToddN

相關問題