🌐
My Tec Bits
mytecbits.com β€Ί tools β€Ί encoders β€Ί xml-encoder
XML Encoder Tool | My Tec Bits.
July 23, 2021 - XML Encoder Tool a free online tool to encode your text having XML's 5 predefined enteties to corresponding escape characters.
🌐
Online XML Tools
onlinexmltools.com β€Ί url-encode-xml
URL-encode XML - Online XML Tools
Simple, free and easy to use online tool that URL-encodes XML. No ads, popups or nonsense, just an XML URL-encoder. Load XML, get URL-escaped XML.
🌐
Online-domain-tools
xml-encoding.online-domain-tools.com
XML Encoding – Easily encode or decode strings or files online
Code page Encoder converts text data from one encoding to another one. Note that source code page for text inputs is always UTF-8. If you want to use another source code page, please use file input. XML Encoder encodes all characters with their corresponding XML entities if such entity exists.
🌐
Zickty
zickty.com β€Ί xmlencode
XML Encode Online Tool
A tool for encoding plain text to XML.
🌐
URL Encode
urlencoder.org β€Ί enc β€Ί xml
URL Encoding of "xml" - Online
Encode xml to URL-encoded format with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
JSON Formatter
jsonformatter.org β€Ί xml-url-encode
Best XML URL Encode Online
Online XML URL Encode tool to convert XML to URL encoded.
🌐
Zickty
zickty.com β€Ί xmldecode
XML Decode Online Tool
A tool for decoding XML to plain text.
Find elsewhere
🌐
Online XML Tools
onlinexmltools.com β€Ί url-decode-xml
URL-decode XML - Online XML Tools
Simple, free and easy to use online tool that URL-decodes XML. No ads, popups or nonsense, just an XML URL-unescaper. Load URL-encoded XML, get URL-decoded XML.
🌐
Code Beautify
codebeautify.org β€Ί xml-url-encoding
XML URL Encoding to url encode XML
XML URL Encoded is easy to use tool to Encode Plain XML data with URL encoding.
🌐
Code Beautify
codebeautify.org β€Ί xml-url-decoding
XML URL Decoding to url decode XML
XML URL Decoding is easy to use tool to Decode XML data which are encoded with URL encoding.
🌐
Base64Encode.org
base64encode.org β€Ί enc β€Ί xml
Base64 Encoding of "xml" - Online
Encode xml to Base64 format with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
My Tec Bits
mytecbits.com β€Ί tools β€Ί encoders β€Ί xml-decoder
XML Decoder Tool | My Tec Bits.
July 23, 2021 - XML Decoder Tool is a free online tool to decode your already encoded text having escape codes for XML's 5 predefined enteties.
🌐
Online XML Tools
onlinexmltools.com β€Ί convert-xml-to-base64
Convert XML to Base64 - Online XML Tools
Free online XML to Base64 converter. Just load your XML and it will automatically get encoded to Base64. There are no ads, popups or nonsense, just an awesome XML to Base64 encoder. Load XML, get Base64.
🌐
URL Decode
urldecoder.org β€Ί dec β€Ί xml
URL Decoding of "xml" - Online
Decode xml from URL-encoded format with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
Browserling
browserling.com β€Ί tools β€Ί xml-to-base64
XML to Base64 Converter - Encode XML to Base64 - Online - Browserling Web Developer Tools
Useful, free online tool that converts XML to base64. No ads, nonsense or garbage, just an XML base64 encoder. Press button, get result.
Top answer
1 of 13
80

Depending on how much you know about the input, you may have to take into account that not all Unicode characters are valid XML characters.

Both Server.HtmlEncode and System.Security.SecurityElement.Escape seem to ignore illegal XML characters, while System.XML.XmlWriter.WriteString throws an ArgumentException when it encounters illegal characters (unless you disable that check in which case it ignores them). An overview of library functions is available here.

Edit 2011/8/14: seeing that at least a few people have consulted this answer in the last couple years, I decided to completely rewrite the original code, which had numerous issues, including horribly mishandling UTF-16.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

/// <summary>
/// Encodes data so that it can be safely embedded as text in XML documents.
/// </summary>
public class XmlTextEncoder : TextReader {
    public static string Encode(string s) {
        using (var stream = new StringReader(s))
        using (var encoder = new XmlTextEncoder(stream)) {
            return encoder.ReadToEnd();
        }
    }

    /// <param name="source">The data to be encoded in UTF-16 format.</param>
    /// <param name="filterIllegalChars">It is illegal to encode certain
    /// characters in XML. If true, silently omit these characters from the
    /// output; if false, throw an error when encountered.</param>
    public XmlTextEncoder(TextReader source, bool filterIllegalChars=true) {
        _source = source;
        _filterIllegalChars = filterIllegalChars;
    }

    readonly Queue<char> _buf = new Queue<char>();
    readonly bool _filterIllegalChars;
    readonly TextReader _source;

    public override int Peek() {
        PopulateBuffer();
        if (_buf.Count == 0) return -1;
        return _buf.Peek();
    }

    public override int Read() {
        PopulateBuffer();
        if (_buf.Count == 0) return -1;
        return _buf.Dequeue();
    }

    void PopulateBuffer() {
        const int endSentinel = -1;
        while (_buf.Count == 0 && _source.Peek() != endSentinel) {
            // Strings in .NET are assumed to be UTF-16 encoded [1].
            var c = (char) _source.Read();
            if (Entities.ContainsKey(c)) {
                // Encode all entities defined in the XML spec [2].
                foreach (var i in Entities[c]) _buf.Enqueue(i);
            } else if (!(0x0 <= c && c <= 0x8) &&
                       !new[] { 0xB, 0xC }.Contains(c) &&
                       !(0xE <= c && c <= 0x1F) &&
                       !(0x7F <= c && c <= 0x84) &&
                       !(0x86 <= c && c <= 0x9F) &&
                       !(0xD800 <= c && c <= 0xDFFF) &&
                       !new[] { 0xFFFE, 0xFFFF }.Contains(c)) {
                // Allow if the Unicode codepoint is legal in XML [3].
                _buf.Enqueue(c);
            } else if (char.IsHighSurrogate(c) &&
                       _source.Peek() != endSentinel &&
                       char.IsLowSurrogate((char) _source.Peek())) {
                // Allow well-formed surrogate pairs [1].
                _buf.Enqueue(c);
                _buf.Enqueue((char) _source.Read());
            } else if (!_filterIllegalChars) {
                // Note that we cannot encode illegal characters as entity
                // references due to the "Legal Character" constraint of
                // XML [4]. Nor are they allowed in CDATA sections [5].
                throw new ArgumentException(
                    String.Format("Illegal character: '{0:X}'", (int) c));
            }
        }
    }

    static readonly Dictionary<char,string> Entities =
        new Dictionary<char,string> {
            { '"', "&quot;" }, { '&', "&amp;"}, { '\'', "&apos;" },
            { '<', "&lt;" }, { '>', "&gt;" },
        };

    // References:
    // [1] http://en.wikipedia.org/wiki/UTF-16/UCS-2
    // [2] http://www.w3.org/TR/xml11/#sec-predefined-ent
    // [3] http://www.w3.org/TR/xml11/#charsets
    // [4] http://www.w3.org/TR/xml11/#sec-references
    // [5] http://www.w3.org/TR/xml11/#sec-cdata-sect
}

Unit tests and full code can be found here.

2 of 13
35

SecurityElement.Escape

documented here

🌐
JSON Formatter
jsonformatter.org β€Ί xml-url-decode
Best XML URL Decode Online
Online XML URL Decode tool to convert XML to URL decoded.
🌐
Code Beautify
codebeautify.org β€Ί xml-escape-unescape
XML Escape and XML Unescape Online Tool
XML Escaper Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari. Vanilla XML Try it. <?xml version="1.0" encoding="UTF-8" ?> <InsuranceCompanies> <Top_Insurance_Companies> <Name>Berkshire Hathaway ( BRK.A)</Name> ...