Reading Xml files with umlaut characters

I asked this question yesterday and received an answer.

Writing encoded values ​​for umlauts

The code uses the parse method if it is like a string:

XDocument xDoc = XDocument.Parse("<description>Top Shelf-ÖÄÜookcase</description>"); 

To pass the input xml file as a string, I must first read it. The read method will not work if there is umlauts in the input xml. How do I get past this?

Tried the Load and Parse XDocument methods.

Load: Invalid character in this encoding. Line 3, position 35. Parse: Data at the root level is invalid. Line 1, position 1.

Here is an xml example after using CDATA:

 <?xml version="1.0" encoding="utf-8"?> <kal> <description><![CDATA[Top Shelf-ÖÄÜookcase]]> </description> </kal> 
+4
source share
3 answers

Change the encoding to "iso-8859-1"

+3
source

Have you tried wrapping these descriptions with CDATA ?

 <description><![CDATA[Top Shelf-ÖÄÜookcase]]> </description> 

Special characters are not particularly well versed in XML unless you wrap them with CDATA.

0
source

As pointed out by Besi , you must use the correct encoding of the xml file in order to achieve the correct processing of umlauts.

Even if you said that creating the incoming xml file is not in your hand, you can still affect the encoding used for parsing xml using the dedicated StreamReader :

 // create your XDocument XDocument Doc; // setup a StreamReader for your file, specifying the encoding you need using (StreamReader Reader = new StreamReader(@"C:\your-file.xml", System.Text.Encoding.GetEncoding("ISO-8859-1"))) { // PARSE the STRING that is RETURNED from the StreamReader.ReadToEnd()-method Doc = XDocument.Parse(Reader.ReadToEnd()); } 
0
source

All Articles