2012-09-19 67 views
4

我得到這個錯誤,迄今爲止我發現的所有內容都是「刪除空間」,但沒有空間。這是一個我發現的腳本,將採取從任何文件格式的恢復和提取數據(解析它)爲了把它放入一個SQL數據庫...還沒有得到那麼遠獲取System.Xml.XmlException:名稱不能以「'字符開頭,十六進制值爲0x20。 42行,位置36

ASP.net代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResumeParser.aspx.cs"  Inherits="CsharpSamCodeResumeParser_USAResume" Debug="true" ValidateRequest="false" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
<title>Untitled Page</title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
    <table style="width: 574px; height: 95px"> 
     <tr> 
      <td style="width: 507px"> 
       Resume 
       URL</td> 
      <td style="width: 737px"> 
       <asp:TextBox ID="TxtUrl" runat="server" Width="424px"></asp:TextBox> 
      </td> 
     </tr>    
     <tr> 
      <td colspan="2" align="center">     
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Resume parser" /></td> 
      </tr> 
     </table> 

    <table> 
     <tr> 
      <td style="width: 247px; height: 287px"> 
       PARSING RESULTS</td> 
      <td style="width: 568px; height: 287px"> 
       <asp:TextBox ID="TxtOutput" runat="server" Height="276px" TextMode="MultiLine" Width="565px"></asp:TextBox></td> 
     </tr> 
    </table> 

</div> 
</form> 

C#:

public partial class CsharpSamCodeResumeParser_USAResume : System.Web.UI.Page 
{ 
//////////Configuration Setting/////////////////// 
string ServiceUrl = (string)ConfigurationSettings.AppSettings["webServiceUrl"]; 
protected void Page_Load(object sender, EventArgs e) 
{ 

} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    ///////////////////Variable Start/////////////// 
    string url = TxtUrl.Text;   
    StringBuilder strRequest =new StringBuilder(); 
    string userkey = (string)ConfigurationSettings.AppSettings["userKey"]; 
    string version = (string)ConfigurationSettings.AppSettings["Version"]; 
    string countryKey=(string)ConfigurationSettings.AppSettings["CountryKey"]; 
    /////////////////Variable End/////////////////// 


    strRequest.Append("<?xml version='1.0' encoding='utf-8'?>"); 
    strRequest.Append("<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"); 
    strRequest.Append("<soap:Body>"); 
    strRequest.Append("<ParseResume xmlns='http://tempuri.org/'>"); 
    strRequest.Append("<url>" + url + "</url>"); 
    strRequest.Append("<key>" + userkey + "</key>"); 
    strRequest.Append("<version>" + version + "</version>"); 
    strRequest.Append("<countryKey>" + countryKey + "</countryKey>"); 
    strRequest.Append("</ParseResume>"); 
    strRequest.Append("</soap:Body>"); 
    strRequest.Append("</soap:Envelope>"); 
    ///////////////SOAP XML END/////////////////// 


    /////////////////XML PROCESSED////////////////////// 
    byte[] byteArray = Encoding.ASCII.GetBytes(strRequest.ToString()); 
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(ServiceUrl); 
    httpRequest.Credentials = CredentialCache.DefaultCredentials; 
    httpRequest.Method = "POST"; 
    httpRequest.ContentType = "text/xml"; 
    httpRequest.ContentLength = byteArray.Length; 
    Stream dataStream = httpRequest.GetRequestStream(); 
    dataStream.Write(byteArray, 0, byteArray.Length); 
    dataStream.Close(); 
    /////////////////////PROCESS END//////////////// 

    //////////////////RESPONSE RECEIVED/////////////////////// 
    HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); 
    StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8); 
    string ack = string.Empty; 
    //ack = streamReader.ReadToEnd().ToString(); 
    ////////////////////GET NODE VALUE FROM RESPONSE XML///////////// 
    using (XmlReader reader = XmlReader.Create(streamReader)) 
    { 
     while (reader.Read()) 
     { 

      if (reader.NodeType == XmlNodeType.Element) 
      { 

       for (int count = 1; count <= reader.Depth; count++) 
       { 
        if (reader.Name == "ParseResumeResult") 
        { 
         ack = reader.ReadElementContentAsString(); 
        } 

       } 

      } 
     } 

     TxtOutput.Text = ack; 
    } 
    /////////////////API CALL PROCESS END/////////////////// 

} 

} 

回答

6

我懷疑是因爲你是通過字符串連接創建你的xml文檔,你無意中從你的一個變量中添加了畸形的xml。也許其中一個有一個小於號,這是導致XML解析器炸彈。您應該嘗試通過DOM創建文檔,或者使用CDATA部分來設置可配置的值。

string url = TxtUrl.Text;   
string userkey = (string)ConfigurationSettings.AppSettings["userKey"]; 
string version = (string)ConfigurationSettings.AppSettings["Version"]; 
string countryKey=(string)ConfigurationSettings.AppSettings["CountryKey"];  

strRequest.Append("<url>" + url + "</url>"); 
strRequest.Append("<key>" + userkey + "</key>"); 
strRequest.Append("<version>" + version + "</version>"); 
strRequest.Append("<countryKey>" + countryKey + "</countryKey>"); 

我會嘗試寫strRequest StringBuilder的一個文件,然後只需在XML編輯器中打開它,並跟蹤問題的所在。

+0

感謝seth,我解決了我的問題與您的建議。 – NevenHuynh

0

你有矛盾的說法在這裏:

strRequest.Append("<?xml version='1.0' encoding='utf-8'?>"); 

VS

byte[] byteArray = Encoding.ASCII.GetBytes(strRequest.ToString()); 

你可能搞砸了編碼和不想要的字符結束。您應該用Encoding.UTF8代替。

+0

改變了它...仍然得到相同的錯誤 – user1574685

+0

你檢查了你的輸入變量嗎?也許他們需要修剪和URL編碼。 – jv42

+0

...我愚蠢的感謝您的幫助(得到的代碼離線和一些填寫空白需要輸入)感謝您的幫助 – user1574685

相關問題