获取 System.Xml.XmlException:名称不能以字符 ' ' 开头,十六进制值为 0x20。行42,位置36。

5

我遇到了这个错误,目前找到的唯一解决方法是“移除空格”,但是代码中并没有空格。这段代码是我找到的一个脚本,可以从任何文件格式中提取简历数据(解析),以便将其放入SQL数据库中...但我还没有做到那一步。

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///////////////////

}

}
2个回答

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

这里有矛盾的陈述:

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

对比

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

你可能会搞砸编码,最终得到不想要的字符。

你应该将 Encoding.ASCII 替换为 Encoding.UTF8


你检查过输入变量了吗?也许它们需要被修剪和 URL 编码。 - jv42
我太蠢了,感谢你的帮助(代码是从网上找到的,需要填写一些空白部分),谢谢你的帮助。 - user1574685

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接