Created
March 31, 2023 15:17
-
-
Save luojunyuan/0731a513a02444d27d77a6f553de0002 to your computer and use it in GitHub Desktop.
csharp .net xml serializer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static T? XmlDeserializer<T>(string text) | |
{ | |
var serializer = new XmlSerializer(typeof(T)); | |
using var reader = new StringReader(text); | |
var result = (T?)serializer.Deserialize(reader); | |
return result; | |
} | |
public static string XmlSerializer<T>(T inst) | |
{ | |
var serializer = new XmlSerializer(typeof(T)); | |
var settings = new XmlWriterSettings { OmitXmlDeclaration = true }; | |
var ns = new XmlSerializerNamespaces(); | |
ns.Add(string.Empty, string.Empty); | |
using var writer = new StringWriter(); | |
using var xmlWriter = XmlWriter.Create(writer, settings); | |
serializer.Serialize(xmlWriter, inst, ns); | |
return writer.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment