473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XML parsing error: Invalid unicode character value for this platform

I've seen a couple other posts on this but no real answers. I'm
trying to do a bulk insert and everything is fine until I run the
objCom.ExecuteNonQuery() statement at which point I get the XML
parsing error. Of course, it would be easy to simply wrap unicode
characters with CDATA <![CDATA[ &#xD;&#xA ]]> but is this doable when
passing values to fields to create a row since a whole row is treated
as a single element? I.e.:

<FileProperty File_ID_FK=\"80\" Property_Name_VC=\"Categories\"
Property_Value=\"\" Property_DataType_VC=\"olText\" />\r\n

Is there a way to set encoding to UTF-8? Any wisdom on this would be
greatly appreciated.

(Below is the offending code where I get my error)

Best,

- Rich

************************************************** ****
static void SaveThroughXML(System.Data.DataSet objDS,
System.Data.SqlClient.SqlConnection objCon)
{
DataTable tbl = objDS.Tables["PSTFiles"];
System.Text.StringBuilder sb = new System.Text.StringBuilder( 1000
);
System.IO.StringWriter sw = new System.IO.StringWriter(sb);

foreach( DataColumn col in tbl.Columns)
{
col.ColumnMapping = System.Data.MappingType.Attribute;
}

objDS.WriteXml(sw, System.Data.XmlWriteMode.WriteSchema);

SqlCommand objCom = new SqlCommand();
objCom.Connection = objCon;
objCom.CommandType = CommandType.StoredProcedure;
objCom.CommandText = "sp_InsertBlah";

objCom.Parameters.Add( new SqlParameter( "@data",
System.Data.SqlDbType.NText));
objCom.Parameters[0].Value = sb.ToString();;
objCom.ExecuteNonQuery(); // Exception is raised here
}
Nov 12 '05 #1
3 9160
I ran into a similar in a different scenario. I was able to solve it by
passing XmlTextWriter with ASCIIEncoding & passing it to the WriteXml method
of the Dataset object.

Something like ...
*****************
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xmlw = new XmlTextWriter(sw, new ASCIIEncoding());
...
...
objDs.WriteXml(xmlw, System.Data.XmlWriteMode.WriteSchema)
*******************
-Naraen
------------
"RichW" <ri*******@hotmail.com> wrote in message
news:d0*************************@posting.google.co m...
I've seen a couple other posts on this but no real answers. I'm
trying to do a bulk insert and everything is fine until I run the
objCom.ExecuteNonQuery() statement at which point I get the XML
parsing error. Of course, it would be easy to simply wrap unicode
characters with CDATA <![CDATA[ &#xD;&#xA ]]> but is this doable when
passing values to fields to create a row since a whole row is treated
as a single element? I.e.:

<FileProperty File_ID_FK=\"80\" Property_Name_VC=\"Categories\"
Property_Value=\"\" Property_DataType_VC=\"olText\" />\r\n

Is there a way to set encoding to UTF-8? Any wisdom on this would be
greatly appreciated.

(Below is the offending code where I get my error)

Best,

- Rich

************************************************** ****
static void SaveThroughXML(System.Data.DataSet objDS,
System.Data.SqlClient.SqlConnection objCon)
{
DataTable tbl = objDS.Tables["PSTFiles"];
System.Text.StringBuilder sb = new System.Text.StringBuilder( 1000
);
System.IO.StringWriter sw = new System.IO.StringWriter(sb);

foreach( DataColumn col in tbl.Columns)
{
col.ColumnMapping = System.Data.MappingType.Attribute;
}

objDS.WriteXml(sw, System.Data.XmlWriteMode.WriteSchema);

SqlCommand objCom = new SqlCommand();
objCom.Connection = objCon;
objCom.CommandType = CommandType.StoredProcedure;
objCom.CommandText = "sp_InsertBlah";

objCom.Parameters.Add( new SqlParameter( "@data",
System.Data.SqlDbType.NText));
objCom.Parameters[0].Value = sb.ToString();;
objCom.ExecuteNonQuery(); // Exception is raised here
}


Nov 12 '05 #2
Naraen -

Thanks so much for your response!

I am trying to implement this and am getting the following errors on the statement:

XmlTextWriter xmlw = new XmlTextWriter(sw, new ASCIIEncoding());

Specifically, on the sw parameter:

Argument '1' cannot convert from 'System.IO.StringWriter' to 'System.IO.Stream'

I get sw from:

StringBuilder sb = new StringBuilder( 1000 );
StringWriter sw = new StringWriter(sb);

Any ideas?

All the best,

- Rich
Nov 12 '05 #3
Rich,
Looks like XmlTextWriter supports the overload for streams but not for
stringwriter.

The best bet would to convert would be to explicitly use the ASCIIEncoding.

E.g.
...
string sUnicodeParm = sb.ToString();
ASCIIEncoding ascii = new ASCIIEncoding();
Byte[] encodedBytes = ascii.GetBytes(unicodeString);
String sAsciiParam = ascii.GetString(encodedBytes);
objCom.Parameters[0].Value = sAsciiParam;
...

-Naraen

------
"RichW" <ri*******@hotmail.com> wrote in message
news:d0*************************@posting.google.co m...
Naraen -

Thanks so much for your response!

I am trying to implement this and am getting the following errors on the statement:
XmlTextWriter xmlw = new XmlTextWriter(sw, new ASCIIEncoding());

Specifically, on the sw parameter:

Argument '1' cannot convert from 'System.IO.StringWriter' to 'System.IO.Stream'
I get sw from:

StringBuilder sb = new StringBuilder( 1000 );
StringWriter sw = new StringWriter(sb);

Any ideas?

All the best,

- Rich

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bekkali Hicham | last post by:
hi, i have already used xalan several times with success, but i have a error message that i don't understand, thanks for your help (Emplacement inconnu de l'erreur) Erreur XSLT...
9
by: Safalra | last post by:
The idea here is relatively simple: a java program (I'm using JDK1.4 if that makes a difference) that loads an HTML file, removes invalid characters (or replaces them in the case of common ones...
0
by: Zengfa Gao | last post by:
Hi all, I try to put into some data I backuped. export PGPASSWORD=security cat /opt/myproj/bin/mysave | /usr/bin/psql -h localhost -U myuser DB_Name I got:
1
by: Chief | last post by:
I am unable to load an xml document that contains Chinese characters in an attribute value. I need to load the document into and XmlDocument object and am using the XmlDocument.Load(string...
5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
12
by: damjan | last post by:
This may look like a silly question to someone, but the more I try to understand Unicode the more lost I feel. To say that I am not a beginner C++ programmer, only had no need to delve into...
1
by: qbp90x5lb | last post by:
I'm using an XSLT transform to output the element value contents from a simple XML file into a new .TXT file. Everything works fine except for certain XML files, when calling msxsl with the .xslt, I...
2
by: ghostrider | last post by:
I've been trying to clear these error messages that I keep getting, was wondering if someone could help me out here. 1. Value of type '1-dimensional array of String' cannot be converted to...
10
by: himanshu.garg | last post by:
Hi, The following std c++ program does not output the unicode character.:- %./a.out en_US.UTF-8 Infinity:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.