Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Xml;
  7. using System.Xml.Linq;
  8. using System.Xml.Serialization;
  9.  
  10. namespace Translatory1
  11. {
  12.     class Program
  13.     {
  14.         public static string GetXMLFromObject(object o)
  15.         {
  16.             StringWriter sw = new StringWriter();
  17.             XmlTextWriter tw = null;
  18.             try
  19.             {
  20.                 XmlSerializer serializer = new XmlSerializer(o.GetType());
  21.                 tw = new XmlTextWriter(sw);
  22.                 serializer.Serialize(tw, o);
  23.             }
  24.             catch (Exception ex)
  25.             {
  26.                 //Handle Exception Code
  27.             }
  28.             finally
  29.             {
  30.                 sw.Close();
  31.                 if (tw != null)
  32.                 {
  33.                     tw.Close();
  34.                 }
  35.             }
  36.             return sw.ToString();
  37.         }
  38.  
  39.         static void Main(string[] args)
  40.         {
  41.             //AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(10));
  42.             try
  43.             {
  44.                 using (StreamReader sr = new StreamReader("text.txt"))
  45.                 {
  46.                     string regex = "(?<id>[0-9]+)," +
  47.                         "(?<productCode>[A-Z]+-[0-9]+),"
  48.                         + "(?<productName>[a-zA-z\\s]+),"
  49.                         + "(?<standardCost>[0-9]+.[0-9]+),"
  50.                         + "(?<listPrice>[0-9]+.[0-9]+),"
  51.                         + "(?<reorderLevel>[0-9]+),"
  52.                         + "(?<targetLevel>[0-9]+),"
  53.                         + "(?<quantityPerUnit>([a-zA-Z\\d\\s]+)|([a-zA-Z\\d\\s-.]+)),"
  54.                         + "(?<discontinued>[0-9]+),"
  55.                         + "(?<minimumReorderQuantity>[0-9]+),"
  56.                         + "(?<category>[a-zA-Z]+)";
  57.  
  58.                     string regex2 = @"^(\s+)(?<id>\d+)(\s+)(?<productCode>\w+-\d+)(\s+)(?<productName>[a-zA-z\s+]+)(\s+)(?<standardCost>\d+.\d+)(\s+)(?<listPrice>\d+.\d+)(\s+)(?<reorderLevel>\d+)(\s+)(?<targetLevel>\d+)(\s+)(?<quantityPerUnit>(((([0-9\.-]+\s)|([a-zA-Z\.]+\s)|([a-zA-Z\.]+))+))([a-zA-Z\.][^0]+))(?<discontinued>[0-9]+)(\s+)(?<minimumReorderQuantity>[0-9]+)(\s+)(?<category>\w+)";
  59.  
  60.                     string regexJson = @"(?<id>[0-9]+)(.*?)(?<productCode>[A-Z]+-[0-9]+)(.*?)(?<productName>[A-Z][a-zA-Z\s]+)(.*?)(?<standardCost>[0-9.]+)(.*?)(?<listPrice>[0-9.]+)(.*?)(?<reorderLevel>\d+)(.*?)(?<targetLevel>\d+)(.*?)(?<quantityPerUnit>(\d+)(([a-zA-Z\d\s]+)|([a-zA-Z\d\s-.]+)))(.*?)(?<discontinued>\d+)(.*?)(?<minimumReorderQuantity>\d+)(.*?)(?<category>[A-Z][a-z]+)";
  61.  
  62.                     var productsList = new List<Product>();
  63.                     while (sr.Peek() >= 0)
  64.                     {
  65.                         String lines = sr.ReadLine();
  66.                         var matches2 = Regex.Matches(lines, regex2);
  67.                         var matches = Regex.Matches(lines, regex);
  68.                         var matchesJson = Regex.Matches(lines, regexJson);
  69.                         foreach (Match match in matches)
  70.                         {
  71.                             var product = new Product()
  72.                             {
  73.                                 Id = Convert.ToInt16(match.Groups["id"].Value),
  74.                                 ProductCode = match.Groups["productCode"].Value,
  75.                                 ProductName = match.Groups["productName"].Value,
  76.                                 StandardCost = match.Groups["standardCost"].Value,
  77.                                 ListPrice = match.Groups["listPrice"].Value,
  78.                                 ReorderLevel = Convert.ToInt16(match.Groups["reorderLevel"].Value),
  79.                                 TargetLevel = Convert.ToInt16(match.Groups["targetLevel"].Value),
  80.                                 QuantityPerUnit = match.Groups["quantityPerUnit"].Value,
  81.                                 Discontinued = Convert.ToInt16(match.Groups["discontinued"].Value),
  82.                                 MinimumReorderQuantity = Convert.ToInt16(match.Groups["minimumReorderQuantity"].Value),
  83.                                 Category = match.Groups["category"].Value,
  84.                             };
  85.                             productsList.Add(product);
  86.                         }
  87.  
  88.                         foreach (Match match in matches2)
  89.                         {
  90.  
  91.                             var product = new Product()
  92.                             {
  93.                                 Id = Convert.ToInt16(match.Groups["id"].Value),
  94.                                 ProductCode = match.Groups["productCode"].Value,
  95.                                 ProductName = match.Groups["productName"].Value,
  96.                                 StandardCost = match.Groups["standardCost"].Value,
  97.                                 ListPrice = match.Groups["listPrice"].Value,
  98.                                 ReorderLevel = Convert.ToInt16(match.Groups["reorderLevel"].Value),
  99.                                 TargetLevel = Convert.ToInt16(match.Groups["targetLevel"].Value),
  100.                                 QuantityPerUnit = match.Groups["quantityPerUnit"].Value,
  101.                                 Discontinued = Convert.ToInt16(match.Groups["discontinued"].Value),
  102.                                 MinimumReorderQuantity = Convert.ToInt16(match.Groups["minimumReorderQuantity"].Value),
  103.                                 Category = match.Groups["category"].Value,
  104.                             };
  105.                             productsList.Add(product);
  106.                         }
  107.  
  108.                         foreach (Match match in matchesJson)
  109.                         {
  110.  
  111.                             var product = new Product()
  112.                             {
  113.                                 Id = Convert.ToInt16(match.Groups["id"].Value),
  114.                                 ProductCode = match.Groups["productCode"].Value,
  115.                                 ProductName = match.Groups["productName"].Value,
  116.                                 StandardCost = match.Groups["standardCost"].Value,
  117.                                 ListPrice = match.Groups["listPrice"].Value,
  118.                                 ReorderLevel = Convert.ToInt16(match.Groups["reorderLevel"].Value),
  119.                                 TargetLevel = Convert.ToInt16(match.Groups["targetLevel"].Value),
  120.                                 QuantityPerUnit = match.Groups["quantityPerUnit"].Value,
  121.                                 Discontinued = Convert.ToInt16(match.Groups["discontinued"].Value),
  122.                                 MinimumReorderQuantity = Convert.ToInt16(match.Groups["minimumReorderQuantity"].Value),
  123.                                 Category = match.Groups["category"].Value,
  124.                             };
  125.                             productsList.Add(product);
  126.                         }
  127.                     }
  128.                     var text = GetXMLFromObject(productsList);
  129.                     File.WriteAllText("Write.xml", text);
  130.                 }
  131.             }
  132.             catch (Exception e)
  133.             {
  134.                 Console.WriteLine("The file could not be read:");
  135.                 Console.WriteLine(e.Message);
  136.             }
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement