Skip to content

Instantly share code, notes, and snippets.

@ncksol
Last active November 3, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncksol/29bd6490edd0580c25f7338b417b37d3 to your computer and use it in GitHub Desktop.
Save ncksol/29bd6490edd0580c25f7338b417b37d3 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication3
{
public class MyObject
{
public string MyString { get; set; }
}
class Program
{
static void Main(string[] args)
{
var fact = new XmlSerializerFactory();
var ser = fact.CreateSerializer(typeof(MyObject));
var obj0 = new MyObject();
obj0.MyString = "H\r\nW" + (char) 3;
var sw = new StringWriter();
ser.Serialize(sw, obj0);
var xml = sw.ToString();
Console.WriteLine(xml);
var sr2 = new StringReader(xml);
var xr2 = new MyXmlTextReader(sr2);
xr2.Normalization = false;
try
{
var obj2 = (MyObject)ser.Deserialize(xr2);
Console.WriteLine("Success : {0}", obj2.MyString);
}
catch (Exception e)
{
Console.WriteLine("Error : {0}", e.InnerException);
}
var sr3 = new StringReader(xml);
var xr3 = new MyXmlTextReader(sr3);
xr3.Normalization = true;
try
{
var obj3 = (MyObject) ser.Deserialize(xr3);
Console.WriteLine("Success : {0}", obj3.MyString);
}
catch (Exception e)
{
Console.WriteLine("Error : {0}", e.InnerException);
}
}
}
class MyXmlTextReader : XmlTextReader
{
public MyXmlTextReader(TextReader input) : base(input)
{
}
/// <summary>
/// Settings
/// </summary>
public override XmlReaderSettings Settings
{
get { return new XmlReaderSettings {CheckCharacters = false}; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment