internal static class ParseHelpers { private static JavaScriptSerializer json; private static JavaScriptSerializer Json { get { return json ?? (json = new JavaScriptSerializer()); } } public static Stream ToStream(this string @this) { var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(@this); writer.Flush(); stream.Position = 0; return stream; } public static T ParseXml<T>(this string @this) where T : class { var reader = XmlReader.Create(@this.Trim().ToStream(), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document }); return new XmlSerializer(typeof(T)).Deserialize(reader) as T; } public static T ParseJson<T>(this string @this) where T : class { return Json.Deserialize<T>(@this.Trim()); } public static string ToOnlyAlphaNumericInput(this string input) { if (input == null) { return null; } return Regex.Replace(input, @"[^\w]", string.Empty); } public static string ToOnlyNormalTextInput(this string input) { if (input == null) { return null; } return Regex.Replace(input, @"[^\w\.@!? ,/:+()'´-]", string.Empty); } }
Tuesday, October 22, 2019
XML, C# - Parse To XML Example
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment