Hi,
Basically I'd like to read xml string from file.
XElement myfile = XElement.Load(@"File.xml"); //And I'm using this to get the content.
PS When I try to open my xml file directly on IE, I get: "An invalid character was found in text content. Error processing resource"
Apparently my xml file has no version or encoding declared and it contains some non geographic characters. Here's example:
XElement international_char = myattribute.Element("international_char");
string mystring = (string)string.Attribute("mystring");
mylabel.text = mystring;
When I open the app it exits with the exception: "invalid characters in the given encoding".
But, I tried to add StreamReader.Encoded.UTF-8(mystring) and my app loads successfully, string also seems to appear just about right.
Well, Some of those characters is being displayer fine. However some aren't. For example these 'eeUu' and possibly some others is being displayed as 'eeUu'.
I really wonder, how can I receive the whole string exactly as it appear on xml file?
By the way, I heard that I could possibly create XML declaration inside my app before reading xml file. But unsure, how does it work.
// Create an XML declaration.
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0",null,null);
xmldecl.Encoding="UTF-8";
xmldecl.Standalone="yes";
Please, can somebody help me and tell me, how to resolve this issue?
Thank you in advance.
Tommy

VulpesPosted Feb 6, 2013, 11:48 AM
However, I was able to get around it like this:
VulpesPosted Feb 17, 2013, 4:55 PM
TommyPosted Feb 17, 2013, 12:49 PM
Hi Vulpes,
I sincerely apologize for my late response. Your solution worked really well. Previously never used non English chars and wasn't really aware how StreamReader can help.
Xixixi, you right. I was silly enough not to close the SR connection and it ended up firing exception. However, with closing and few more IF statements it works as it intend to. Now few bits and bobs and app shall be ready.
Again, massive THANK YOU for your help and for sharing your knowledge with me. It has helped so much. You are No1, honestly :))
Greetings,
Tommy
VulpesPosted Feb 7, 2013, 11:24 AM
If not, then the StreamReader constructor may fail to open the file again, because it's still locked.
One good thing about File.ReadAllText is that it automatically closes the file.
I was thinking that the encoding might be UTF-8 because that's the commonest for XML files, in my experience, and because Windows-1252 (Western European ANSI) didn't seem to be working properly. However, if Encoding.Default which uses your system's current code page (probably 1252 in any case) is working OK, then I'd stick with that.
TommyPosted Feb 6, 2013, 7:34 PM
I see. But then again, why 3rd party app cannot update xml file since I use StreamReader encoding? I got the feeling it somehow locks my xml file (sort of: new Tommys app already using xml file, can't update it). But when I don't use StreamReader then it works fine.
I am unsure, this must be happening due to encoding then?! Just don't know what to do. Only wish my app was be able to renew xml file and same time I load xml content.
Vulpes, Why do you believe the content might be UTF-8?
By the way, when GetEncoding(0) // I actually chose to have Default then everything is being displayed as it suppose to. Well, only just this one main problem with reloading xml.
Xml file itself definitely can display all chars correctly, do OS Localisation is not an issue then.
I must somehow sort out my app to overcome this issue. But how?! Not easy :))
VulpesPosted Feb 6, 2013, 7:03 PM
For example, if you're displaying it to the console using raster fonts, this is what you'd get:
This is my string ZESiecUueC èéÓäùcG and so on
Notice that the inverted circumflex accent can't be displayed over the Z and S but everything else looks OK.
This is why I used the Windows Forms MessageBox in my example where I knew that the font would be able to display these characters.
If you don't specify an encoding in the StreamReader constructor, then UTF-8 should be used by default.
TommyPosted Feb 6, 2013, 4:54 PM
I've just done few tests. Here is what I get, and it really drives me nuts :))
Here's what happening.
My xml file is generated by the 3rd party application and it automatically changes the content every 3 mins (each time recreate the file).
Please note, file content and any non geographic chars is displayed correctly inside myfile.xml.
Now, I open my app (lets say file currently contain normal chars). It all looks fine.
Next I have prepared xml content with non geographic chars, which shall be renewed as soon 3rd party changed that.
Timer detects that and reloads new xml content on app (usually, as soon 3 mins countdown stopped()). Label.text now can display non geographic chars (it also is working fine).
But that's where all my troubles start.
As soon current content from file expires, the timer again reloads and update new content of myfile.xml (it can be anything, normal or non geographic).
However, because I use "StreamReader(@"File.xml", Encoding.GetEncoding(1252));" for some unknown reason 3rd party app doesn't update myfile.xml content on it any longer.
I have the feeling it's something to do with this line. Because when I close my app or comment that line it works as intend to.
What's wrong with using StreamReader? How can I overcome this possibly really little thing?
Vulpes, what do you think is wrong?
Honestly, embarrassed to ask this much but I feel thankful for help.
VulpesPosted Feb 6, 2013, 2:56 PM
TextReader tr = new StreamReader(@"File.xml", Encoding.GetEncoding(1252));
XElement mystring = XElement.Load(tr);
TommyPosted Feb 6, 2013, 2:29 PM
Hi Vulpes,
Good to hear from you :))
I'll possibly repeat myself, but massive thank you again for the code and your help.
I'd say you might be right, because my xml has no declaration it might be in plain ANSI.
However, do you remember XElement.Load function that you previously helped me with?
Well, I'd really like to get it work under the same loader function. Have you got any idea, how can I implement Encoding.GetEncoding() while still using XElement.Load to load my xml file?
I've tried multiple ways to get it work but I'm stuck. By using StreamReader Encoding.GetEncoding(), somehow this operator doesn't allow for my 3rd party app to generate that xml file.
It does seem that I managed to make things work and I am so close. Now left with few small errors to overcome.
Thank you again million times!!!!
VulpesPosted Feb 6, 2013, 10:50 AM
There looks to be some errors in the way you're reading that particular fragment of XML but even so the error message seems odd :-/