This line doesn't make sense:
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:
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.