Getting System.Xml.XmlException: The name cannot begin with the character '', the hexadecimal value 0x20. Line 42, position 36

I get this error, and all I have found so far is “delete space” but there is no space. This is a found script that will take a resume from any file format and extract the data (parse it) to put it in the SQL database ... haven't gotten to this yet

ASP.net Code:

<%@ 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> 

WITH#:

 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/////////////////// } } 
+4
source share
2 answers

I suspect that since you are creating your XML document using string concatenation, you accidentally add the wrong xml from one of your variables. Perhaps one of them has less sign, which makes the Xml parser bomb. You should try to create the document through the DOM or, possibly, using CDATA sections for custom values.

 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 to write the strRequest StringBuilder file to a file, and then just open it in an xml editor and track where the problem is.

+6
source

You have conflicting statements here:

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

vs

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

You can ruin the encodings and end up with unwanted characters.

You should replace Encoding.ASCII with Encoding.UTF8 .

0
source

Source: https://habr.com/ru/post/1435084/


All Articles