Tommy

Tommy

  • NA
  • 134
  • 26.2k

How can I resolve xml read and encode problems?

Feb 6 2013 10:11 AM


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:

<mystring>This is my string ŽEŠiecUueC èéÓäùcG and so on</mystring> //As you can see my string contains some non geographic characters.

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

 

 


Answers (10)