• c# xml序列化和反序列化


            static public string SerializeToXml<T>(T obj)
            {
                string xmlString = string.Empty;
                //XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                //using (MemoryStream ms = new MemoryStream())
                //{
                //    xmlSerializer.Serialize(ms, obj);
                //    xmlString = Encoding.UTF8.GetString(ms.ToArray());
                //}
                Encoding encoding = Encoding.UTF8;
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
                    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                    namespaces.Add("", "");
    
                    XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, encoding);
    
                    xmlTextWriter.Formatting = Formatting.None;
                    xmlSerializer.Serialize(xmlTextWriter, obj, namespaces);
                    xmlTextWriter.Flush();
                    xmlTextWriter.Close();
    
                    xmlString = encoding.GetString(memoryStream.ToArray());
                }
                return xmlString;
            }
    
            static public T DeserializeXml<T>(string xmlString)
            {
                T t = default(T);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
                {
                    using (XmlReader xmlReader = XmlReader.Create(xmlStream))
                    {
                        Object obj = xmlSerializer.Deserialize(xmlReader);
                        t = (T)obj;
                    }
                }
                return t;
            }
    
            static public string VerifyXml(string xml)
            {
                string result = string.Empty;
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(xml);
                }
                catch (XmlException ex) { result = ex.Message; }
                return result;
            }
    
            static public string GetXmlNodeText(string xml, string nodeName)
            {
                string result = string.Empty;
                try
                {
                    var doc = new XmlDocument();
                    doc.LoadXml(xml);
                    XmlNode entityName = doc.SelectSingleNode(nodeName);
                    if (entityName != null)
                    {
                        result = entityName.InnerText.Trim();
                    }
                }
                catch { }
                return result;
            }
  • 相关阅读:
    el-table单元格新增、编辑、删除功能
    div 拖拽
    dataTable 分页用法总结
    pc端字体大小计算以及echart中字体大小计算
    pc端字体大小自适应几种方法
    AB PLC教程
    Electric p8激活教程
    三菱FX系列PLC教程
    Visual Studio 2015简体中文企业版/专业版下载+有效激活密钥
    servlet在mvc里的分层,驻留内存的疑问?
  • 原文地址:https://www.cnblogs.com/hofmann/p/11756968.html
Copyright © 2020-2023  润新知