Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class mySerializer
- {
- public static string ObjectToXML<T>(T obj)
- {
- using (StringWriter stringWriter = new StringWriter(new StringBuilder()))
- {
- XmlSerializer xmlSerializer = GetSerializer<T>();
- XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
- namespaces.Add(string.Empty, string.Empty);
- XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
- xmlWriterSettings.OmitXmlDeclaration = true; // remove <?xml ... >
- xmlWriterSettings.Indent = true;
- xmlWriterSettings.IndentChars = " ";
- xmlWriterSettings.NewLineChars = "\r\n";
- xmlWriterSettings.NewLineHandling = NewLineHandling.Replace;
- XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings);
- xmlSerializer.Serialize(xmlWriter, obj, namespaces);
- return stringWriter.ToString();
- }
- }
- public class NamespaceIgnorantXmlTextReader : XmlTextReader
- {
- public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader) : base(reader) { }
- public override string NamespaceURI
- {
- get { return ""; }
- }
- }
- // helper class to omit XML decl at start of document when serializing
- public class XTWFND : XmlTextWriter
- {
- public XTWFND(System.IO.TextWriter w) : base(w) { Formatting = System.Xml.Formatting.Indented; }
- public override void WriteStartDocument() { }
- }
- public static string RemoveNamespaces(string xml)
- {
- XElement Xml = XElement.Parse(xml);
- foreach (XElement XE in Xml.DescendantsAndSelf())
- {
- // Stripping the namespace by setting the name of the element to it's localname only
- XE.Name = XE.Name.LocalName;
- // replacing all attributes with attributes that are not namespaces and their names are set to only the localname
- XE.ReplaceAttributes((from xattrib in XE.Attributes().Where(xa => !xa.IsNamespaceDeclaration) select new XAttribute(xattrib.Name.LocalName, xattrib.Value)));
- }
- return Xml.ToString();
- }
- public static T ObjectFromXML<T>(string xml)
- {
- // xml = RemoveNamespaces(xml);
- using (StringReader stringReader = new StringReader(xml))
- {
- // using (NamespaceIgnorantXmlTextReader xreader = new NamespaceIgnorantXmlTextReader(stringReader))
- // {
- //xreader.Namespaces = false;
- XmlSerializer serializer = GetSerializer<T>();
- return (T)serializer.Deserialize(stringReader);
- // }
- }
- }
- public static T ObjectFromXML<T>(StringBuilder xml)
- {
- using (StringBuilderReader stringReader = new StringBuilderReader(xml))
- {
- XmlSerializer serializer = GetSerializer<T>();
- return (T)serializer.Deserialize(stringReader);
- }
- }
- public static T ObjectFromXMLInFile<T>(string filePath)
- {
- using (StreamReader sr = new StreamReader(filePath))
- {
- XmlSerializer serializer = GetSerializer<T>();
- return (T)serializer.Deserialize(sr);
- }
- }
- public static T ObjectFromJson<T>(string input)
- {
- return JsonConvert.DeserializeObject<T>(input);
- }
- public static string ObjectToJson(object obj)
- {
- return JsonConvert.SerializeObject(obj, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
- }
- public static T ObjectFromJson<T>(string input, JsonSerializerSettings settings)
- {
- return JsonConvert.DeserializeObject<T>(input, settings);
- }
- public static string ObjectToJson(object obj, Newtonsoft.Json.Formatting formatting, JsonSerializerSettings settings)
- {
- return JsonConvert.SerializeObject(obj, formatting, settings);
- }
- private static ConcurrentDictionary<System.Type, XmlSerializer> serializers = new ConcurrentDictionary<System.Type, XmlSerializer>();
- public static XmlSerializer GetSerializer<T>()
- {
- var type = typeof(T);
- XmlSerializer result;
- if (serializers.TryGetValue(type, out result)) return result;
- var attributes = type.GetCustomAttributes(typeof(XmlTypeAttribute), false);
- var attributeOverride = new XmlAttributeOverrides();
- if (attributes.Length > 0)
- {
- var ns = ((XmlTypeAttribute)attributes[0]).Namespace;
- attributeOverride.Add(type,
- new XmlAttributes
- {
- Xmlns = false,
- XmlRoot = new XmlRootAttribute
- {
- Namespace = ns,
- // Namespace = string.Empty,
- ElementName = type.Name.Replace("Type", "")
- }
- });
- }
- result = new XmlSerializer(type);
- serializers.TryAdd(type, result);
- return result;
- }
- static public string BeautifyXml(string xml)
- {
- try
- {
- var stringBuilder = new StringBuilder();
- var element = XElement.Parse(xml);
- var settings = new XmlWriterSettings();
- settings.OmitXmlDeclaration = true;
- settings.Indent = true;
- settings.NewLineOnAttributes = true;
- using (var xmlWriter = XmlWriter.Create(stringBuilder, settings))
- {
- element.Save(xmlWriter);
- }
- element = null;
- return stringBuilder.ToString();
- }
- catch
- {
- return xml;
- }
- }
- }
- protected static bool XmlGetNode(string sXml, string sNodeName, out string XmlFound)
- {
- bool retval = false;
- XmlFound = string.Empty;
- string sTest = string.Empty;
- sXml = Regex.Replace(sXml, "(\r)|(\n)|(\t)", string.Empty, RegexOptions.IgnoreCase);
- if (sNodeName.Length > 0 && sXml.Length > 0)
- {
- sTest = Regex.Replace(sXml, string.Format(@"^.*\<{0}", sNodeName), string.Format(@"<{0}", sNodeName), RegexOptions.IgnoreCase);
- //sTest = Regex.Replace(sTest,@String.Format(@"{0}\>.*$",sNodeName), String.Format(@"{0}>",sNodeName), RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
- sTest = Regex.Replace(sTest, string.Format(@"</{0}>.*$", sNodeName), string.Format(@"</{0}>", sNodeName), RegexOptions.IgnoreCase);
- sTest = Xattribute(sTest);
- }
- if (sTest.Length > 0)
- {
- retval = true;
- XmlFound = sTest;
- }
- return retval;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement