对象转化为 xml字符串

public static string ToXml<T>(this T o) where T : new()
{
        string retVal;
        using (var ms = new MemoryStream())
        {
              var xs = new XmlSerializer(typeof (T));
               xs.Serialize(ms, o);
               ms.Flush();
               ms.Position = 0;
               var sr = new StreamReader(ms);
               retVal = sr.ReadToEnd();
        }
        return retVal;
}

 

posted @ 2015-07-21 09:15  Coding_Yong  阅读(283)  评论(0编辑  收藏  举报