Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
XmlException in XmlSerializer.Deserialize when XML has \0
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Xml; using System.IO; using System.Text; using System.Xml.Serialization; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { string teste="Hello, world!\0\0\0\0"; //Your code goes here Console.WriteLine(teste + teste.Length); var xml = SerializeObject<Teste>(new Teste() { teste = teste//.Replace("\0","") }); Console.WriteLine(xml); var outp = DeserializeObject<Teste>(xml); Console.WriteLine(outp.teste); } public static string SerializeObject<T>(T toSerialize) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); using(StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, toSerialize); return textWriter.ToString(); } } public static T DeserializeObject<T>(string txt) where T: class, new() { XmlSerializer xmlSerializer = new XmlSerializer(new T().GetType()); MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(txt)); return xmlSerializer.Deserialize(ms) as T; } } public class Teste { public string teste; } }
run
|
edit
|
history
|
help
0
math 5.74
operator overriding
c# abstract
yari
Interface fully qualified names
Sum of numbers in string
Datum_CRM
Test DateTimeStyles.AdjustToUniversal
Download a UTF-8 text file, and display the contents.
makingNum