tags:

views:

288

answers:

1

Hi,

I am trying to load an XML which has special characters such as ö and ® into XElement. But I get an error saying "Invalid character in the given encoding". Where do I set the encoding so that I will be able to load the string?

A: 

You could set the encoding in the first line of the XML file:

<?xml version="1.0" encoding="utf-8"?>

Could you show your XML file?

Darin Dimitrov
this is how the xml looks like<page> <title>some title</title> <text pbname="body_text" pballowhtml="any"><![CDATA[<P><LI>OS Windows® 2000, Windows® XP Professional, Windows® XP Home Edition</LI></UL>]]></text> <a pbname="rss_link" href=""/> <text pbname="faq_catagories_field">Systems Integration</text></page>i will try adding the xml encoding statement at the top. Thanks..
Amit
No sir adding the above line to the xml did not solve the issue.. :(
Amit
The XML looks valid and it loads fine with `XElement.Load("test.xml")`.
Darin Dimitrov
Now I saved the xml file with encoding statement in selecting the UTF-8 encoding. The file loaded up properly. but on writing it to a file, this is how the text came upWindows® 2000I capture the string from the read xml based on some logic and write it to a file with StreamWriter. How can I retain the proper characters..?
Amit
`new StreamWriter(stream, Encoding.UTF8)`
Darin Dimitrov
Hey thanks... here is the code for writing using (StreamWriter objStreamWriter = new StreamWriter(fileName,true,Encoding.UTF8,512)) { objStreamWriter.Write(fileContent); objStreamWriter.Flush(); objStreamWriter.Close(); }
Amit
Hey one more thing UTF-8 will not support text such as BÖWE notice the Ö... its in CAPS how can that be considered?
Amit