Saving Linq file in Xml as ANSI instead of UTF-8 in C # (Ivy)

In C #, I need to create XML files for use with Ivy and NAnt, but I find it difficult to get the correct encoding in the output file.

If I use XElement.Save ("C: \ foo.xml"), I get a correctly looking file, but Ivy and / or NAnt are upset, since the file is actually saved using UTF-8, but I really need to save it as ANSI to be able to use it.

I currently have a binding that needs to use .ToString () to get the text, and then use StreamWriter to write to the file.

Ideally, I would like to set the format during .Save (), but cannot find any information about this.

Thank.

+3
source share
1 answer

XDocument.Save has a number of overloads.

Writing through a properly constructed XmlWriter allows us to choose ASCII encoding.

var doc = XDocument.Parse( "<foo>&#160;bar&#7800;baz</foo>" );

XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new ASCIIEncoding();
using (var writer = XmlWriter.Create( "xelement.xml", settings )) {
    doc.Save( writer );
}
+10
source

Source: https://habr.com/ru/post/1732566/


All Articles