• XmlHelper.cs


            public class XMLHelper
            {
                public static string Serialize<T>(T entity, Encoding encoding = null)
                {
                    StringBuilder sb = new StringBuilder();
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
    
                    if (encoding == null)
                    {
                        using (TextWriter tw = new StringWriter(sb))
                            serializer.Serialize(tw, entity);
                        return sb.ToString();
                    }
    
                    using (MemoryStream ms = new MemoryStream())
                    using (StreamWriter sw = new StreamWriter(ms, encoding))
                    {
                        serializer.Serialize(sw, entity);
                        byte[] bytes = ms.ToArray();
                        return encoding.GetString(bytes, 0, bytes.Length);
                    }
                }
    
                public static void AppendSerializeNameSpace(XmlDocument doc)
                {
                    if (string.IsNullOrWhiteSpace(doc.InnerText)) return;
                    if (!doc.DocumentElement.HasAttribute("xmlns:xsi"))
                        doc.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
                    if (!doc.DocumentElement.HasAttribute("xmlns:xsd"))
                        doc.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
                }
    
                public static void SerializeToFile<T>(string filePath, T obj, string rootName = null)
                {
                    using (StreamWriter sw = new StreamWriter(filePath))
                    {
                        XmlSerializer serializer = string.IsNullOrWhiteSpace(rootName) ?
                            new XmlSerializer(typeof(T))
                            : new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
                        serializer.Serialize(sw, obj);
                    }
                }
    
                public static T DeserializeFromFile<T>(string filePath)
                {
                    using (StreamReader sr = new StreamReader(filePath))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(T));
                        return (T)serializer.Deserialize(sr);
                    }
                }
    
                public static T Deserialize<T>(string xmlString)
                {
                    T cloneObject = default(T);
    
                    StringBuilder buffer = new StringBuilder();
                    buffer.Append(xmlString);
    
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
    
                    using (TextReader reader = new StringReader(buffer.ToString()))
                    {
                        Object obj = serializer.Deserialize(reader);
                        cloneObject = (T)obj;
                    }
    
                    return cloneObject;
                }
    
            }
  • 相关阅读:
    在IIS上发布Web(使用VS2005)
    ASP.NET Web Service应用发布到IIs怎么做
    (转)在 Visual Studio 2010 中创建 ASP.Net Web Service
    matlab函数之diag
    Coursera《machine learning》--(6)逻辑回归
    UFLDL教程(五)之self-taught learning
    UFLDL教程之(三)PCA and Whitening exercise
    matlab的常用快捷键
    matlab函数之bsxfun
    Coursera《machine learning》--(14)数据降维
  • 原文地址:https://www.cnblogs.com/RedSky/p/16112739.html
Copyright © 2020-2023  润新知