I have the following test case:
[TestMethod]
public void SimpleEncodingTest()
{
var report = new SimpleReport{Title = @"[quote]""[/quote] [apo]'[/apo] [smaller]<[/smaller] [bigger]>[/bigger] [and]&[/and]" };
XmlSerializer xsSubmit = new XmlSerializer(typeof(SimpleReport));
var xml = "";
using (var sww = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sww, new XmlWriterSettings
{
Encoding = Encoding.Default
}))
{
xsSubmit.Serialize(writer, report);
xml = sww.ToString(); // Your XML
}
}
}
I want all special characters including the quotes at apostrophe to be included as such:
<?xml version="1.0" encoding="utf-16" ?>
<SimpleReport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>[quote]"[/quote] [apo]'[/apo] [smaller]<[/smaller] [bigger]>[/bigger] [and]&[/and]</Title>
</SimpleReport>
With the title being "[quote]"[/quote] [apo]'[/apo] [smaller]<[/smaller] [bigger]>[/bigger] [and]&[/and]"
Instead I get:
<?xml version="1.0" encoding="utf-16" ?>
<SimpleReport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>[quote]"[/quote] [apo]'[/apo] [smaller]<[/smaller] [bigger]>[/bigger] [and]&[/and]</Title>
</SimpleReport>
And the title is [/quote] [apo]'[/apo] [smaller]<[/smaller] [bigger]>[/bigger] [and]&[/and].
How do I tell the serializer that I have quotes and apostrophes encoded as well?
PS: I know you don't typically need to encode these characters but this is a client requirement.
Attempts:
tried providing settings such as: Avoid XML Escape Double Quote but it did not change outcome
Tried to change encoding to UTF-8 and other encodings without success
https://www.codeproject.com/Questions/1249846/How-do-you-force-Csharp-xmlserializer-to-escape-ap
Tried using System.Net.WebUtility.HtmlDecode(string). However, System.Net.WebUtility.HtmlDecode(string) does not encode quotes and apostrophe.
Tried using SecurfityElement.Escape(string). This correctly translated the string to
"
serializer then translate that to&quot;
.