Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all .
I have a string with a XML text and i want to save it like XML. I encoded string (to "utf-8") but when i want to make XML from that - my cyrillic symbols in Value don't displayed right . What i need to do to encode my XML document ?

part of my xml :
XML
<rev:Code>Мои данные</rev:Code>


my code:
C#
 string send = Encoding.GetEncoding("utf8").GetString(Encoding.GetEncoding("utf-8").GetBytes(send)); 
XmlDocument docsec = new XmlDocument();
 docsec.LoadXml(send);
docsec.Save("C:\\XmlNEW.xml");



Original text :Мои данные

I see it after creating XML :Мои данные
Posted
Updated 10-Apr-15 2:25am
v2
Comments
Joan Magnet 10-Apr-15 8:41am    
Take a look at this good article about encoding unicode, maybe the font you use to view doesn't support unicode.

http://www.codeproject.com/Articles/885262/Reading-and-writing-Unicode-data-in-NET

This line doesn't make sense:
C#
string send = Encoding.GetEncoding("utf-8").GetString(Encoding.GetEncoding("utf-8").GetBytes(send));

You see in .NET String is a collection of Unicode characters (or in better words a collection of Unicode code points) that represent a text which is encoded by UTF-16 encoding.
So in other words you already have the desired text representation.

Now what I presume could cause your problem is either you have specified an XmlDeclaration that defines some other (non utf-8) encoding, or maybe that declaration is read from the XML text that you are loading, or maybe you are saving the file in some other manner in which case the problem is probably with that code.
If you read the remarks of XmlDocument.Save(String) Method[^] you will notice that it will use encoding attribute which is taken from the XmlDeclaration.Encoding property.

Nevertheless can you try the following:
C#
XmlDocument docsec = new XmlDocument();
docsec.LoadXml(send);
using (TextWriter writer = new StreamWriter("C:\\XmlNEW.xml", false, Encoding.UTF8))
    docsec.Save(writer);

If your issue remains then the problem could be with the send variable's content itself, you should debug it and see what content it stores.
Is it a desired text? If not then the problem is with the code that is retrieving that value.
 
Share this answer
 
Comments
Sascha Lefèvre 10-Apr-15 9:27am    
5ed.
Andrey Golubtsov 10-Apr-15 9:53am    
Now its clear . My text displayed as i want . Thanks alot !
Problem was in Save Method because it use xml encoding(as you say) .
Sergey Alexandrovich Kryukov 10-Apr-15 12:16pm    
5ed.
—SA
Sergey Alexandrovich Kryukov 10-Apr-15 12:35pm    
There is also one delicate moment related to prolog and BOM. Please see Solution 2.
—SA
Maciej Los 10-Apr-15 14:03pm    
+5
In addition to Solution 1:

The content of XML should also be consistent with the actually applied encoding. With UTF-8, this is how your prolog should look:
XML
<?xml version="1.0" encoding="UTF-8"?>


There is one delicate thing here: the BOM. First, read about it:
http://en.wikipedia.org/wiki/BOM,
http://unicode.org/glossary,
http://unicode.org/faq/utf_bom.html.

The UTFs in regular text files using Unicode are recognized by modern text editors by BOM and only by BOM. The XML processors also always use it. But in case of UTF-8 and prolog, even without BOM the UTF is correctly detected. How? This is because UTF-8 gives you exact same bytes as ASCII when all code points of the characters fall in ASCII range of code points (more exactly, code points 32 to 127). So, the prolog is read using ASCII, and then the rest of document is read in the specified encoding.

In all cases, prolog and optional BOM must be consistent with actual encoding used. :-)

—SA
 
Share this answer
 
v6
Comments
Maciej Los 10-Apr-15 14:05pm    
Nice to know ;)
+5!
Sergey Alexandrovich Kryukov 10-Apr-15 14:57pm    
Thank you, Maciej.
—SA
Mario Z 10-Apr-15 15:20pm    
Really nice addition, +5!
Sergey Alexandrovich Kryukov 10-Apr-15 15:25pm    
Thank you, Mario.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900