XML常用类(淘宝API)

mac2022-06-30  108

  1  using  System;   2  using  System.Collections.Generic;   3  using  System.Linq;   4  using  System.Text;   5  using  System.Xml;   6  using  System.Xml.Serialization;   7  using  System.IO;   8    9  namespace  TaoBao.API.Common  10  {  11       ///   <summary>  12       ///  操作XML常用方法集合  13       ///   </summary>  14       public   class  XMLCommon  15      {  16   17           ///   <summary>  18           ///  将XML文档转换成实例对象  19           ///   </summary>  20           ///   <typeparam name="T"> 对象类型 </typeparam>  21           ///   <param name="pmFileName"> 文件名 </param>  22           ///   <returns> 实例对象 </returns>  23           public   static  T DeserializeFile < T > ( string  pmFileName)  24          {  25              FileStream fs  =   new  FileStream(pmFileName, FileMode.Open);  26              XmlSerializer xs  =   new  XmlSerializer( typeof (T));  27              T tObjext  =  (T)xs.Deserialize(fs);  28              fs.Close();  29              fs.Dispose();  30               return  tObjext;  31          }  32            33           ///   <summary>  34           ///  将实例对象写入XML文档  35           ///   </summary>  36           ///   <typeparam name="T"> 对象类型 </typeparam>  37           ///   <param name="pmFileName"> 文件名 </param>  38           ///   <param name="pmT"> 实例对象 </param>  39           public   static   void  SerializeFile < T > ( string  pmFileName, T pmT)  40          {  41   42              XmlSerializer serializer  =   new  XmlSerializer(pmT.GetType());  43              TextWriter writer  =   new  StreamWriter(pmFileName);  44              serializer.Serialize(writer, pmT);  45              writer.Close();  46          }  47     48   49           ///   <summary>  50           ///  将XML字符串转换成对象  51           ///   </summary>  52           ///   <typeparam name="T"> 对象类型 </typeparam>  53           ///   <param name="pmXMLString"> XML字符串 </param>  54           ///   <returns> 实例对象 </returns>  55           public   static  T DeserializeXML < T > ( string  pmXMLString) {  56   57              XmlSerializer xs  =   new  XmlSerializer( typeof (T));  58   59              T tObjext  =  (T)xs.Deserialize( new  StringReader(pmXMLString));  60               return  tObjext;  61          }  62   63   64           public   static   string  SerializeString < T > (T pmObject) {  65   66              StringBuilder sb  =   new  StringBuilder();  67              XmlSerializer xc  =   new  XmlSerializer(pmObject.GetType());  68              System.IO.MemoryStream memory  =   new  MemoryStream();  69              System.Xml.XmlTextWriter xmltext  =   new  System.Xml.XmlTextWriter(memory,Encoding.Default);  70              xc.Serialize(xmltext, pmObject);  71              xmltext.Close();  72              memory.Close();  73   74               string  reValue  = Encoding.Default.GetString(memory.GetBuffer());  75   76               return  reValue;  77          }  78           ///   <summary>  79           ///  将Josn字符串转换成对象  80           ///   </summary>  81           ///   <typeparam name="T"> 对象类型 </typeparam>  82           ///   <param name="pmXMLString"> Josn字符串 </param>  83           ///   <returns> 实例对象 </returns>  84           public   static  T DeserializeJSON < T > ( string  pmXMLString)  85          {  86              XmlSerializer xs  =   new  XmlSerializer( typeof (T));  87              T tObjext  =  (T)xs.Deserialize( new  StringReader(pmXMLString));  88               return  tObjext;  89          }  90   91   92           ///   <summary>  93           ///  将实例对象转换成键值对集合  94           ///   </summary>  95           ///   <typeparam name="T"> 对象类型 </typeparam>  96           ///   <param name="pmT"> 实例对象 </param>  97           ///   <returns> 键值对集合 </returns>  98           public   static  Dictionary < string , string >  DeserializeClass < T > (T pmT)  99          { 100               // UTF8Encoding utf8 = new UTF8Encoding(); 101               // Dictionary<string, string> reValue = new Dictionary<string, string>(); 102               // string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff")+"xmlfile.xml"; 103               // SerializeFile(filename, pmT); 104               // FileStream fs = new FileStream(filename, FileMode.Open); 105               // XmlDocument document = new XmlDocument(); 106               // document.Load(fs); 107               // foreach(XmlNode child in document.DocumentElement.ChildNodes){ 108  109               //     reValue.Add(child.Name, utf8.GetString(utf8.GetBytes(child.InnerText))); 110               // } 111               // fs.Close(); 112               // fs.Dispose(); 113               /// /File.Delete(filename); 114               // return reValue; 115  116              UTF8Encoding utf8  =   new  UTF8Encoding(); 117              Dictionary < string string >  reValue  =   new  Dictionary < string string > (); 118              XmlDocument document  =   new  XmlDocument(); 119              document.LoadXml(SerializeString < T > (pmT)); 120               foreach  (XmlNode child  in  document.DocumentElement.ChildNodes) 121              { 122                  reValue.Add(child.Name, utf8.GetString(utf8.GetBytes(child.InnerText))); 123              } 124               return  reValue; 125          } 126  127           ///   <summary> 128           ///  将键值对集合转成字符串 129           ///   </summary> 130           ///   <param name="pmDictionary"> 键值对集合 </param> 131           ///   <returns> 字符串 </returns> 132           public   static   string  GetParams(IDictionary < string string >  pmDictionary) 133          { 134              System.Collections.IEnumerator iter  =  pmDictionary.Keys.GetEnumerator(); 135              System.Text.StringBuilder orgin  =   new  System.Text.StringBuilder(); 136               int  i  =   0 ; 137               while  (iter.MoveNext()) 138              { 139                   string  name  =  ( string )iter.Current; 140                   string  value  = System.Web.HttpUtility.UrlEncode(pmDictionary[name], System.Text.Encoding.UTF8); 141                  orgin.Append(name).Append( " = " ).Append(value); 142                   if  (i  !=  pmDictionary.Keys.Count  -   1 ) orgin.Append( " & " ); 143                  i ++ ; 144              } 145               return  orgin.ToString(); 146          } 147  148      } 149  } 150 

 

转载于:https://www.cnblogs.com/bbqqqbq/archive/2009/08/20/1550945.html

相关资源:淘宝API的使用与XML使用
最新回复(0)