2009-10-16 215 views
0

嗨,朋友們,希望所有人都做得很好。在閱讀vb.net中的xml文件時遇到問題,即給出錯誤消息"Data at the root level is invalid. Line 860, position 18."我的xml文件在讀取時出錯,如下所示。讀取xml文件時出現錯誤

實際上,我使用以下代碼在0123b的vb.net中保存此文件。

Try 
    Dim strUrl As String = "http://xyz" 
    Dim wr As HttpWebRequest = CType(WebRequest.Create(strUrl), HttpWebRequest) 
    Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse) 
    ws.ContentType = "UTF-8" 
    Dim str As Stream = ws.GetResponseStream() 
    Dim inBuf(100000) As Byte 
    Dim bytesToRead As Integer = CInt(inBuf.Length) 
    Dim bytesRead As Integer = 0 
    While bytesToRead > 0 
     Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead) 
     If n = 0 Then 
      Exit While 
     End If 
     bytesRead += n 
     bytesToRead -= n 
    End While 
    Dim fstr As New FileStream("c:/GetXml.xml", FileMode.OpenOrCreate, FileAccess.Write) 
    fstr.Write(inBuf, 0, bytesRead) 
    str.Close() 
    fstr.Close() 
Catch ex As WebException 
    Response.Write(ex.Message) 
End Try 


<?xml version="1.0" encoding="UTF-8" ?> 
- <Result> 
- <Jobs Found="58" Returned="50"> 
- <Job ID="8000009"> 
    <Title>Water Infrastucture Professionals</Title> 
- <Summary> 
- <![CDATA[ Great Potential for Career Growth Rewardidng Salary package Great Potential for Career Growth Rewardidng Salary package Our client is major utilities entity who is searching for tradespeople with water, sewage, stormwater and related infrastructure network experience. This is an exciting role with excellent career benefits and an opportunity to utilise your extensive experience in field operations 
    ]]> 
    </Summary> 
    <DateActive Date="2009-10-14T11:26:28-05:00">10/14/2009</DateActive> 
    <DateExpires Date="2009-11-13T21:33:33-05:00">11/13/2009</DateExpires> 
    <DateUpdated Date="2009-10-14 21:34:00">10/14/2009</DateUpdated> 
- <Location> 
    <Country>xxx</Country> 
    <State>xxx</State> 
    <City>xxx</City> 
    <PostalCode>00000000</PostalCode> 
    </Location> 
    <CompanyName>Hudson</CompanyName> 
    <BuilderFields /> 
    <DisplayOptions /> 
    <AddressType>6</AddressType> 
    </Job> 
- <Job ID="83678951" PositionID="61132478"> 
    <Title>SENIOR CIVIL ENGINEER - ROADS AND HIGHWAYS</Title> 
- <Summary> 
- <![CDATA[ ARE YOU A CIVIL ENGINEER? THIS IS A RARE AND EXCITING OPPORTUNITY TO PROGRESS YOUR CAREER WITH A WELL ESTABLISHED AND GROWING COMPANY!!! ACT LOCATION EXCELLENT REMUNERATION MULTIPLE PROJECTS Our client is one of the worlds leading engineering consultancies with over forty year&#39;s experience across a wide range of disciplines. With an expanding project portfolio they are now seeking to employ an expe 
    ]]> 
    </Summary> 
    <DateActive Date="2009-09-29T18:02:30-05:00">9/29/2009</DateActive> 
    <DateExpires Date="2009-10-29T23:36:33-05:00">10/29/2009</DateExpires> 
    <DateUpdated Date="2009-09-29 23:37:00">9/29/2009</DateUpdated> 
- <Location> 
    <Country>Australia</Country> 
    <State>ACT</State> 
    <City>CANBERRA</City> 
    <PostalCode>2600</PostalCode> 
    </Location> 
    <CompanyName>Gemteq</CompanyName> 
    <BuilderFields /> 
    <DisplayOptions /> 
    <AddressType>6</AddressType> 
    </Job> 
- <Job ID="83679392" PositionID="61132870"> 
    <Title>Principal Mechanical Engineer</Title> 
- <Summary> 
- <![CDATA[ Canberra based Permanent Role Attractive salary package High Profile Organisation Our client is currently seeking a Principal&#39;s Mechanical Engineer to lead their team of professional mechanical services design engineers. Responsibilities You will be leading a team of engineers to: Develop technical and professional excellence. Use environmentally sustainable design practices. Manage a variety of pro 
    ]]> 
    </Summary> 
    <DateActive Date="2009-09-29T17:57:34-05:00">9/29/2009</DateActive> 
    <DateExpires Date="2009-10-30T00:44:30-05:00">10/30/2009</DateExpires> 
    <DateUpdated Date="2009-09-30 00:45:00">9/30/2009</DateUpdated> 
- <Location> 
    <Country>Australia</Country> 
    <State>ACT</State> 
    <City>Canberra</City> 
    <PostalCode>2600</PostalCode&gThe XML page cannot be displayed 
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. 


-------------------------------------------------------------------------------- 

Invalid at the top level of the document. Error processing resource 'file:///Z:/d8fb0c80-e45c-4207-97b0-47e4d6c314ad.xml'. ... 

</Jobs></Result>> 

t; 
    </Location> 
    <CompanyName>Human Touch Resource Group</CompanyName> 
- <Salary> 
    <Max Value="30000">30,000.00</Max> 

請求。爲我找到解決方案。提前致謝。

+0

@pravakar,你怎麼能堅持你以前的帖子? http://stackoverflow.com/questions/1576534/how-to-read-xml-data-from-a-url-by-using-vb-net-and-save/1576729#1576729 – 2009-10-17 01:54:54

回答

1

你可以試試下面的短版下載文件:

Using client As New System.Net.WebClient 
    client.DownloadFile("http://xyz", "c:/GetXml.xml") 
End Using 
+0

看到這個:http:///stackoverflow.com/questions/1576534/how-to-read-xml-data-from-a-url-by-using-vb-net-and-save/1576729#1576729 – 2009-10-17 01:55:33

0

在寫出來之後,您的XML文件是否長達100,000個字節?如果是這樣,那可能是問題 - 您的代碼目前只能處理長達100,000個字節的文件。不使用一個大緩衝區,最好反覆讀取響應並寫入文件,直到沒有更多數據。這裏有一個C#方法複製流 - 我將其轉換爲VB在一分鐘:如果這問題

public static void CopyStream(Stream input, Stream output) 
{ 
    byte[] buffer = new byte[8192]; 
    int bytesRead; 
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) 
    { 
     output.Write(buffer, 0, bytesRead); 
    } 
} 

,你在文本編輯器中的文件了,得看看行860,位置18?

1

你的代碼有一些缺陷:

  1. 它限制了文件的長度爲100000個字節(如果它不再你將最終得到一個破損的xml)
  2. 異常安全,一些句柄在異常情況下不會關閉,如果在例外後第二次進入這個例程,XML可能被鎖定(取決於GC處置了這條街今年已經與否)

我會改寫循環是這樣的:

  • 獲取webstream
  • 打開文件 - > FILESTREAM
  • 寫入該文件分段
  • 關閉文件&流