Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is mine code to create XML

C++
int _tmain(int argc, _TCHAR* argv[])
{
	char *xmldata = "<?xml version=1.0 encoding=UTF-8?><DataCaptureSesstings><ModuleSettings><account_id>1</account_id><capture_local_dir>c:\</capture_local_dir><capture_log_dir>c:\log</capture_log_dir><capture_log_level>debug</capture_log_level><capture_request_interval>2</capture_request_interval><capture_connection_interval>2</capture_connection_interval><smtp_server_name>n1</smtp_server_name><smtp_server_port>80</smtp_server_port><smtp_email_sender>s1</smtp_email_sender><smtp_email_sender_password>p1</smtp_email_sender_password></ModuleSettings><MachineList><Machine><MachineId>0022</MachineId><Make>Make1</Make><Model>Model1</Model><SerialNumber>SN1</SerialNumber><IpAddress>10.10.10.10</IpAddress><Port>80</Port></Machine><Machine><MachineId>3000</MachineId><Make>Make3</Make><Model>Model3</Model><SerialNumber>SN3</SerialNumber><IpAddress>30.30.30.30</IpAddress><Port>80</Port></Machine></MachineList></DataCaptureSesstings>";
	std::ofstream outfile ("sampleP2.xml");
	
	outfile << xmldata;

	outfile.close();
}


When i open the sampleP2.xml i am getting this error
A string literal was expected, but no opening quote character was found. Error processing resource 'file:///C:/Documents an...

<?xml version=1.0 encoding=UTF-8?><DataCaptureSesstings><ModuleSettings><account_id>1</account_id...

What's the error?
With char* the below solution is fine but what to do with CString str;
Posted
Updated 17-Oct-12 1:40am
v2

1 solution

The version and encoding parameters must be quoted:
C++
char *xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>...";
 
Share this answer
 
Comments
Tarun Batra 17-Oct-12 7:58am    
if i use CString str ="...";
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Invalid at the top level of the document. Error processing resource 'file:///C:/Documents and Settings/Administrator/Deskto...

00154118
Jochen Arndt 17-Oct-12 8:11am    
The first thing to do is opening the created file with a text editor like Notepad to check if the content is as expected.

Is your application an Unicode build? Then you should use CStringA to create a single byte string or convert the string to UTF-8 (e.g. with WideCharToMultiByte()) when the string contains non-ASCII characters. If the string contains non-ASCII characters, you must convert it to UTF-8 because you specified the encoding.
Tarun Batra 17-Oct-12 9:00am    
Can you please tell how to convert BSTR to char *.
Jochen Arndt 17-Oct-12 9:09am    
I already mentioned how to do it: Use WideCharToMultiByte().
int nSize = ::WideCharToMultiByte(CP_UTF8, 0, bstr, -1, NULL, 0, NULL, NULL);
char *lpszUtf8 = new char[nSize];
::WideCharToMultiByte(CP_UTF8, 0, bstr, -1, lpszUtf8, nSize, NULL, NULL);
// lpszUtf8 contains now the UTF-8 encoded string
// outfile << lpszUtf8;
delete [] lpszUtf8;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900