Json|XMl|Object互相转化-小工具

  1 using Newtonsoft.Json;
  2 using System;
  3 using System.IO;
  4 using System.Text;
  5 using System.Xml;
  6 using System.Xml.Serialization;
  7 
  8 public static class XmlConvert
  9     {
 10         public static string SerializXml(object teg) {
 11             StringBuilder sb = new StringBuilder();
 12             StringWriter tw = new StringWriter(sb);
 13             XmlSerializer xmlserializer = new XmlSerializer(teg.GetType());
 14             try
 15             {
 16                 xmlserializer.Serialize(tw, teg);
 17                 tw.Flush();
 18                 tw.Close();
 19                 tw.Dispose();
 20                 return sb.ToString();
 21             }
 22             catch (Exception e)
 23             {
 24                 return string.Empty;
 25             }
 26             finally {
 27                 tw.Flush();
 28                 tw.Close();
 29                 tw.Dispose();
 30             }
 31         }
 32         public static T DeserializXml<T>(string xml) where T:new() {
 33             T result = default(T);
 34             StringReader sr = new StringReader(xml);
 35             XmlSerializer xmlserializer = new XmlSerializer(typeof(T));
 36             result=(T)xmlserializer.Deserialize(sr);
 37             return result;
 38         }
 39         public static string XmlToJson(string xml) {
 40             XmlDocument doc = new XmlDocument();
 41             doc.LoadXml(xml);
 42             return JsonConvert.SerializeXmlNode(doc);
 43         }
 44         public static string JsonToXml(string json,string rootname) {
 45             StringReader sr = new StringReader(json);
 46             JsonTextReader jtr = new JsonTextReader(sr);
 47             StringBuilder sb = new StringBuilder();
 48             StringWriter sw = new StringWriter(sb);
 49             XmlTextWriter xmlwriter = new XmlTextWriter(sw);
 50             try
 51             {
 52                 xmlwriter.WriteStartDocument();
 53                 xmlwriter.WriteStartElement(rootname);
 54                 bool enterArray = false;
 55                 while (jtr.Read())
 56                 {
 57                     switch (jtr.TokenType)
 58                     {
 59                         case JsonToken.StartArray:
 60                             enterArray = true;
 61                             break;
 62                         case JsonToken.EndArray:
 63                             enterArray = false;
 64                             break;
 65                         case JsonToken.StartObject:
 66                             if (enterArray)
 67                                 xmlwriter.WriteStartElement("item");
 68                             break;
 69                         case JsonToken.EndObject:
 70                             xmlwriter.WriteEndElement();
 71                             break;
 72                         case JsonToken.PropertyName:
 73                             xmlwriter.WriteStartElement(jtr.Value.ToString());
 74                             break;
 75                         case JsonToken.String:
 76                         case JsonToken.Integer:
 77                             xmlwriter.WriteCData(jtr.Value.ToString());
 78                             xmlwriter.WriteEndElement();
 79                             break;
 80                         default: break;
 81                     }
 82                 }
 83                 xmlwriter.WriteEndElement();
 84 
 85                 return sb.ToString();
 86             }
 87             catch (Exception e)
 88             {
 89                 return string.Empty;
 90             }
 91             finally {
 92                 xmlwriter.Flush();
 93                 xmlwriter.Close();
 94                 sw.Flush();
 95                 sw.Close();
 96                 jtr.Close();
 97                 sr.Close();
 98                 xmlwriter.Dispose();
 99                 sw.Dispose();
100                 sr.Dispose();
101             }
102         }
103     }

posted on 2017-02-01 21:48  Thancoo  阅读(1312)  评论(0编辑  收藏  举报

导航