Клас для роботи з Yandex Translate API

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using WindowsFormsApplication2.Properties;

namespace WindowsFormsApplication2
{
    public  class YandexTranslate
    {
        private readonly string _apiKey;

        public YandexTranslate()
        {
            _apiKey = Settings.Default.ApiKey;
        }

        public YandexTranslate(string key)
        {
            _apiKey = key;
        }

        public  List<string> GetLangs()
        {
            var requestString =
                String.Format("https://translate.yandex.net/api/v1.5/tr.json/getLangs?key={0}",
                _apiKey);
            var request = WebRequest.Create(requestString);
            var response = request.GetResponse(); 
            var yandexDataContractSerializer = new DataContractJsonSerializer(typeof(GetLangsData));
            var getLangsData = (GetLangsData)yandexDataContractSerializer.ReadObject(response.GetResponseStream());
            return getLangsData.Dirs;
        }
        public  List<string> GetLangs(string ui)
        {
            var requestString =
                String.Format("https://translate.yandex.net/api/v1.5/tr.json/getLangs?key={0}&amp;ui={1}",
                _apiKey, ui);
            var request = WebRequest.Create(requestString);
            var response = request.GetResponse();
            var sr = new StreamReader(response.GetResponseStream());
            var json = sr.ReadToEnd();
            json = json.Remove(json.Length - 3);
            json = json.Substring(json.IndexOf("langs") + 8);
            json = json.Replace('"', ' ');            
            return json.Split(',').ToList();
        }
        public  string Detect(string text)
        {
            var requestString =
                String.Format("https://translate.yandex.net/api/v1.5/tr.json/detect?key={0}&amp;text={1}",
                _apiKey, text);
            var request = WebRequest.Create(requestString);
            var response = request.GetResponse();
            var yandexDataContractSerializer = new DataContractJsonSerializer(typeof(DetectData));
            var detectData = (DetectData)yandexDataContractSerializer.ReadObject(response.GetResponseStream());
            return detectData.Lang;
        }
        public  List<string> Translate(string lang, string text)
        {
            var requestString =
                String.Format("https://translate.yandex.net/api/v1.5/tr.json/translate?key={0}&amp;text={1}&amp;lang={2}&amp;format={3}",
                _apiKey, text, lang, "plain");

            var request = WebRequest.Create(requestString);
            if ((requestString.Length > 10240) &amp;&amp; (request.Method.StartsWith("GET")))
                throw new ArgumentException("Text is too long (>10Kb)");
            var response = request.GetResponse();

            TranslateData translateData;
            var yandexDataContractSerializer = new DataContractJsonSerializer(typeof(TranslateData));
            try
            {
                translateData = (TranslateData)yandexDataContractSerializer.ReadObject(response.GetResponseStream());
            }
            catch (Exception exception)
            {
                translateData = new TranslateData();
                translateData.Text = new List<string>();
            }
            return translateData.Text;
        }
        [DataContract]
        internal class TranslateData
        {
            [DataMember (Name = "code")]
            internal int Code { get; set; }            
            [DataMember(Name = "lang")]
            internal string Lang { get; set; }
            [DataMember(Name = "text")]
            internal List<string> Text { get; set; }
        }
        [DataContract]
        public class GetLangsData
        {
            [DataMember(Name = "dirs")]
            internal List<string> Dirs { get; set; }
        }
        [DataContract]
        internal class DetectData
        {
            [DataMember(Name = "code")]
            internal int Code { get; set; }            
            [DataMember(Name = "lang")]
            internal string Lang { get; set; }
        }
    }
}

Advertisements

Приклад серіалізації колекції ArrayList

Наведу приклад серіалізації колекції ArrayList з власних елементів MyElementClass:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
 
class Program
 {
  
static void Main(string[] args)
   {
    
ArrayList al = new ArrayList();
     XmlSerializer serializer=
new XmlSerializer(typeof(ArrayList),new Type[] {typeof (MyElementClass )});
     al.Add(
new MyElementClass());
     al.Add (
new MyElementClass (2,"AAA"));
    
Console.WriteLine("Writing With Stream");
    
try
    
{
       
Stream writer = new FileStream("C:\A.XML", FileMode.Create);
       
serializer.Serialize(writer, al);
       
writer.Close();
     
}
     
catch (Exception ex)
     
{
       
Console.WriteLine(ex.Message );
     
}
 
}
}

Сам елемент є клас:

using

System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
 
public class MyElementClass: IComparable
  {
   
public int ID;
   
public string Name;
   
public MyElementClass()
    {
      ID = 1;
      Name =
"ABC";
    }
   
public MyElementClass(int id, string name)
    {
      ID = id;
      Name = name;
    }
   
public int CompareTo(object obj)
    {
      
if (obj is MyElementClass)
        {
           MyElementClass otherElement = (MyElementClass)obj;
          
return this.ID.CompareTo (otherElement.ID);
        }
      
else
       
{
         
throw new ArgumentException("object is not a MyElement");
        }
   }
 }
}