C# 获取 System.Xml.XmlException:名称不能以“ ”字符开头,十六进制值 0x20。第 42 行,第 36 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12497718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting System.Xml.XmlException: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 42, position 36
提问by user1574685
I'm getting this error and all i've found so far is "remove the space", but there is no space. It is a script i found that will take a resume from any file format and extract the data (parse it) in order to put it into a SQL database...haven't gotten that far yet
我收到此错误,到目前为止我发现的只是“删除空间”,但没有空间。这是我发现的一个脚本,它将从任何文件格式中获取简历并提取数据(解析它),以便将其放入 SQL 数据库中......还没有那么远
ASP.net Code:
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#:
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///////////////////
}
}
采纳答案by Seth Flowers
I suspect that because you are creating your xml document via string concatenation, that you are inadvertently adding malformed xml from one of your variables. Maybe one of them has a less than sign, which is causing the Xml parser to bomb. You should try creating the document via a DOM, or possibly using CDATAsections for the configurable values.
我怀疑因为您是通过字符串连接创建 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>");
I would try writing the strRequestStringBuilder to a file, and then just open it in an xml editor, and track down where the problem is.
我会尝试将strRequestStringBuilder写入文件,然后在 xml 编辑器中打开它,并找出问题所在。
回答by jv42
You have contradictory statements here:
你在这里有矛盾的陈述:
strRequest.Append("<?xml version='1.0' encoding='utf-8'?>");
vs
对比
byte[] byteArray = Encoding.ASCII.GetBytes(strRequest.ToString());
You might mess up encodings and end up with unwanted chars.
您可能会弄乱编码并最终得到不需要的字符。
You should replace Encoding.ASCIIby Encoding.UTF8.
你应该替换Encoding.ASCII为Encoding.UTF8.

