I’m using XML database which contains names. Unfortunately I’m living in Czech Republic and there are lot of czech letters in czech names. These letters are ě š č ř ž ý á í é ú ů. Application crashes during parsing data everytime when database contains one of these letters.
Debugger shows error:”Invalid character in given encoding. Line 4. Position 15″.
My XML:
<?xml version="1.0" encoding="utf-8"?>
<Players>
<Player>
<Name>Karel Čubera</Name>
</Player>
<Player>
<Name>Jiri Vontubas</Name>
</Player>
<Player>
<Name>Jan Vernovsky</Name>
</Player>
</Players>
My App:
static void Main(string[] args)
{
XDocument xmlDoc = XDocument.Load("C://Database.xml");
var XMLdata = from singlePlayer in xmlDoc.Descendants("Player")
select new
{
Name = singlePlayer.Element("Name").Value,
};
foreach (var singlePlayer in XMLdata)
{
Console.WriteLine("Player name: " + singlePlayer.Name);
Console.WriteLine();
}
Console.Read();
}
Is Linq to XML even able to read these symbols ? I’ve encountered same problem in windows form application. I realy need these leters without them it just sucks. 🙂 Thanks in advance
Your XML document is wrongly encoded. Try opening it in Notepad, do a File->Save As and select UTF-8. Then reopen it and check if everything is correct.
As a sidenote, be aware that the font used in the Console isn’t always able to show all the Unicode characters. If you put a breakpoint you’ll be able to check if the file has been correctly loaded. For example it shows to me
Karel ČuberaasKarel Cubera