zoukankan      html  css  js  c++  java
  • XmlHelper

    public static class XmlHelper
        {
           /// <summary>
           /// 将对象序列化成xml
           /// </summary>
           /// <param name="Obj">对象</param>
           /// <param name="ObjType">类名</param>
           /// <returns></returns>
           public static string ToXml(object Obj, Type ObjType)
           {
               XmlSerializer serializer = new XmlSerializer(ObjType);
               string str = string.Empty;
               using (MemoryStream stream = new MemoryStream())
               {
                   using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
                   {
                       serializer.Serialize((XmlTextWriter)writer,Obj);
                   }
                   str = Encoding.UTF8.GetString(stream.GetBuffer());
                   str = str.Substring(str.IndexOf(Convert.ToChar(60)));
                   return str.Substring(0,str.LastIndexOf(Convert.ToChar(0x3e))+1);
               }
           }
           public static string ToXml(object obj)
           {
               return ToXml(obj, true);
           }
    
           public static string ToXml(object Obj, bool wellFormatted)
           {
               XmlSerializer serializer = new XmlSerializer(Obj.GetType());
               string str = null;
               using (MemoryStream stream = new MemoryStream())
               {
                   using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
                   {
                       if (wellFormatted)
                       {
                           writer.Formatting = Formatting.Indented;
                           writer.Indentation = 4;
                       }
                       serializer.Serialize((XmlWriter)writer, Obj);
                   }
                   str = Encoding.UTF8.GetString(stream.GetBuffer());
                   str = str.Substring(str.IndexOf(Convert.ToChar(60)));
                   return str.Substring(0, str.LastIndexOf(Convert.ToChar(0x3e)) + 1);
               }
           }
           public static void ToXml(object obj, Type objType, Stream stream)
           {
               StreamWriter writer = new StreamWriter(stream);
               try
               {
                   new XmlSerializer(objType).Serialize((TextWriter)writer, obj);
               }
               finally
               {
                   writer.Flush();
               }
           }
           public static void ToXmlFile(object Obj, string fileName)
           {
               string contents = ToXml(Obj);
               File.WriteAllText(fileName, contents, new UTF8Encoding());
           }
           public static void ToXmlFile(object Obj, Type ObjType, string fileName)
           {
               string contents = ToXml(Obj, ObjType);
               File.WriteAllText(fileName, contents, new UTF8Encoding());
           }
           public static void ToXmlFile(object Obj, Type ObjType, string fileName)
           {
               string contents = ToXml(Obj, ObjType);
               File.WriteAllText(fileName, contents, new UTF8Encoding());
           }
    
        }
  • 相关阅读:
    13.字符串压缩算法
    12.字符串全部替换指定文本
    46.数字到字符串的转换
    45.切割字符串并精确分配内存
    44.字符串删除指定字符或者字符串
    11.表达式计算对一串加减乘除带括号进行计算
    43.可变参数实现printf
    关于float与double区别
    float,double与long long哪个更大?
    double 与0比较时有个精度问题,有时需精确到小数点后面几位,例如与>0.0001,而不能与>0比较
  • 原文地址:https://www.cnblogs.com/zhengwei-cq/p/9052340.html
Copyright © 2011-2022 走看看