Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Xml;
- using System.Xml.Linq;
- using System.Xml.Serialization;
- namespace Translatory1
- {
- class Program
- {
- public static string GetXMLFromObject(object o)
- {
- StringWriter sw = new StringWriter();
- XmlTextWriter tw = null;
- try
- {
- XmlSerializer serializer = new XmlSerializer(o.GetType());
- tw = new XmlTextWriter(sw);
- serializer.Serialize(tw, o);
- }
- catch (Exception ex)
- {
- //Handle Exception Code
- }
- finally
- {
- sw.Close();
- if (tw != null)
- {
- tw.Close();
- }
- }
- return sw.ToString();
- }
- static void Main(string[] args)
- {
- //AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(10));
- try
- {
- using (StreamReader sr = new StreamReader("text.txt"))
- {
- string regex = "(?<id>[0-9]+)," +
- "(?<productCode>[A-Z]+-[0-9]+),"
- + "(?<productName>[a-zA-z\\s]+),"
- + "(?<standardCost>[0-9]+.[0-9]+),"
- + "(?<listPrice>[0-9]+.[0-9]+),"
- + "(?<reorderLevel>[0-9]+),"
- + "(?<targetLevel>[0-9]+),"
- + "(?<quantityPerUnit>([a-zA-Z\\d\\s]+)|([a-zA-Z\\d\\s-.]+)),"
- + "(?<discontinued>[0-9]+),"
- + "(?<minimumReorderQuantity>[0-9]+),"
- + "(?<category>[a-zA-Z]+)";
- 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+)";
- 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]+)";
- var productsList = new List<Product>();
- while (sr.Peek() >= 0)
- {
- String lines = sr.ReadLine();
- var matches2 = Regex.Matches(lines, regex2);
- var matches = Regex.Matches(lines, regex);
- var matchesJson = Regex.Matches(lines, regexJson);
- foreach (Match match in matches)
- {
- var product = new Product()
- {
- Id = Convert.ToInt16(match.Groups["id"].Value),
- ProductCode = match.Groups["productCode"].Value,
- ProductName = match.Groups["productName"].Value,
- StandardCost = match.Groups["standardCost"].Value,
- ListPrice = match.Groups["listPrice"].Value,
- ReorderLevel = Convert.ToInt16(match.Groups["reorderLevel"].Value),
- TargetLevel = Convert.ToInt16(match.Groups["targetLevel"].Value),
- QuantityPerUnit = match.Groups["quantityPerUnit"].Value,
- Discontinued = Convert.ToInt16(match.Groups["discontinued"].Value),
- MinimumReorderQuantity = Convert.ToInt16(match.Groups["minimumReorderQuantity"].Value),
- Category = match.Groups["category"].Value,
- };
- productsList.Add(product);
- }
- foreach (Match match in matches2)
- {
- var product = new Product()
- {
- Id = Convert.ToInt16(match.Groups["id"].Value),
- ProductCode = match.Groups["productCode"].Value,
- ProductName = match.Groups["productName"].Value,
- StandardCost = match.Groups["standardCost"].Value,
- ListPrice = match.Groups["listPrice"].Value,
- ReorderLevel = Convert.ToInt16(match.Groups["reorderLevel"].Value),
- TargetLevel = Convert.ToInt16(match.Groups["targetLevel"].Value),
- QuantityPerUnit = match.Groups["quantityPerUnit"].Value,
- Discontinued = Convert.ToInt16(match.Groups["discontinued"].Value),
- MinimumReorderQuantity = Convert.ToInt16(match.Groups["minimumReorderQuantity"].Value),
- Category = match.Groups["category"].Value,
- };
- productsList.Add(product);
- }
- foreach (Match match in matchesJson)
- {
- var product = new Product()
- {
- Id = Convert.ToInt16(match.Groups["id"].Value),
- ProductCode = match.Groups["productCode"].Value,
- ProductName = match.Groups["productName"].Value,
- StandardCost = match.Groups["standardCost"].Value,
- ListPrice = match.Groups["listPrice"].Value,
- ReorderLevel = Convert.ToInt16(match.Groups["reorderLevel"].Value),
- TargetLevel = Convert.ToInt16(match.Groups["targetLevel"].Value),
- QuantityPerUnit = match.Groups["quantityPerUnit"].Value,
- Discontinued = Convert.ToInt16(match.Groups["discontinued"].Value),
- MinimumReorderQuantity = Convert.ToInt16(match.Groups["minimumReorderQuantity"].Value),
- Category = match.Groups["category"].Value,
- };
- productsList.Add(product);
- }
- }
- var text = GetXMLFromObject(productsList);
- File.WriteAllText("Write.xml", text);
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("The file could not be read:");
- Console.WriteLine(e.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement