New to Telerik UI for WinFormsStart a free 30-day trial

How to change the encoding configuration of an SVG file from UTF-16 to UTF-8

Updated over 6 months ago

Environment

Product VersionProductAuthor
2022.2.622PictureBox for WinFormsDinko Krastev

Description

When trying to show an SVG XML file inside the RadPictureBox control, while this file is encoded with the UTF-16 algorithm, an error will be thrown. The RadPictureBox internal reader will throw System.Xml.XmlException: 'There is no Unicode byte order mark. Cannot switch to Unicode.'.

Solution

To make this work we need to change the encoding attribute in the SVG XML file at the top. You can try to manually open the file in an editor and change the encoding attribute to utf-8 or use custom code.

C#

public RadForm1()
{
    InitializeComponent();

    string path = @"..//..//Document.svg";
    string path_new = @"..//..//Document_new.svg";
         
    Encoding utf8 = new UTF8Encoding(false);
    Encoding ansi = Encoding.GetEncoding(1252);
    string xml = File.ReadAllText(path, ansi);
            
    XDocument xmlDoc = XDocument.Parse(xml);
    File.WriteAllText(
        path_new,
        @"<?xml version=""1.0"" encoding=""utf-8""?>" + xmlDoc.ToString(),
        utf8
    );
    this.radPictureBox1.SvgImage = RadSvgImage.FromFile(path);
}
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support