• .net core XML 解析帮助类


        public class XSerializer
        {
            /// <summary>
            /// 将对象序列化为xml字符串
            /// </summary>
            /// <typeparam name="T">类型<peparam>
            /// <param name="t">对象</param>
            public static string ObjectToXml<T>(T t) where T : class
            {
                XmlSerializer formatter = new XmlSerializer(typeof(T));
                using (MemoryStream stream = new MemoryStream())
                {
                    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                    namespaces.Add(string.Empty, string.Empty);
                    formatter.Serialize(stream, t, namespaces);
                    string result = Encoding.UTF8.GetString(stream.ToArray());
                    return result;
                }
            }
            /// <summary>
            /// 序列化成XML 清空格式
            /// </summary>
            public static string ObjectToXml<T>(T t, Encoding encoding) where T : class
            {
                XmlSerializer formatter = new XmlSerializer(typeof(T));
                using MemoryStream stream = new MemoryStream();
                XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                namespaces.Add(string.Empty, string.Empty);
                XmlTextWriter xmlTextWriter = new XmlTextWriter(stream, encoding);
                xmlTextWriter.Formatting = System.Xml.Formatting.None;
                formatter.Serialize(xmlTextWriter, t, namespaces);
                xmlTextWriter.Flush();
                xmlTextWriter.Close();
                string result = encoding.GetString(stream.ToArray());
                return result;
            }
    
            /// <summary>
            /// 字符串转换为对象
            /// </summary>
            public static T XmlToObject<T>(string xml) where T : class
            {
                XmlSerializer formatter = new XmlSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
                {
                    T result = formatter.Deserialize(ms) as T;
                    return result;
                }
            }
        }
    
  • 相关阅读:
    Hive快速入门
    Spark Standalone 提交模式
    Spark WordCount 文档词频计数
    Spark Shuffle原理分析及性能优化
    Spark性能问题分析及优化【OOM、Stack Overflow】
    Redis常用命令【列表】
    Redis常用命令【字符串】
    Nosql数据库分类
    Redis内存数据库快速入门
    Scrapy实现腾讯招聘网信息爬取【Python】
  • 原文地址:https://www.cnblogs.com/rsls/p/14851990.html
Copyright © 2020-2023  润新知