Advertisement
Guest User

pasrer

a guest
May 10th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.68 KB | None | 0 0
  1. //Input: (Read the input parameters from a configuration file.)
  2. //
  3. //    1.Path to the folder with XML files to be processed.
  4. //    2.XPath expression pointing to a node in XML.
  5. //    3.Number of concurrent threads.
  6. //
  7. //Output: Sorted list of all different values that this node has with number of occurrences of each value, sorted by number of //occurrences, descending. If a node is missing in a certain XML it should be considered as having "N/A" value.
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Xml;
  16. using System.Xml.Linq;
  17. using System.Xml.XPath;
  18. using System.Configuration;
  19. using System.Collections.Specialized;
  20.  
  21. namespace AnotherTry
  22. {
  23.     class Program
  24.     {
  25.         static void Main(string[] args)
  26.         {
  27.             NameValueCollection settings = ConfigurationManager.GetSection("AnotherXML.appSettings") as NameValueCollection;
  28.  
  29.             string folder1 = @"C:\XML";
  30.             string folder2 = @"D:\Downloads\XML";
  31.             string folder3 = @"C:\XMLAnother";
  32.  
  33.             string folder = settings["folder"];
  34.  
  35.             string xpath1 = "docID";
  36.             string xpath2 = "sort/@s_c";
  37.             string xpath3 = "book";
  38.             string xpath4 = "protein/name";
  39.             string xpath5 = "industrySet";
  40.  
  41.             string xpath = settings["xpath"];
  42.  
  43.             int threadNum = Convert.ToInt32(settings["threads"]);
  44.  
  45.             string[] xmlFiles = Directory.GetFiles(folder, "*.xml");
  46.  
  47.             Dictionary<string, int> dict = new Dictionary<string, int>();
  48.  
  49.             foreach (var path in xmlFiles)
  50.             {
  51.                 bool isFound = false;
  52.                 bool isFoundInFile = false;
  53.  
  54.                 long fileSize = new FileInfo(path).Length;
  55.  
  56.                 XmlReaderSettings xmlSettings = new XmlReaderSettings();
  57.                 xmlSettings.DtdProcessing = DtdProcessing.Parse;                
  58.  
  59.                 using (XmlReader reader = XmlReader.Create(path, xmlSettings))
  60.                 {
  61.                     if (fileSize > 10000000)
  62.                     {
  63.                         reader.ReadStartElement();
  64.                     }
  65.                     //reader.MoveToContent();
  66.  
  67.                     // Parse the file and return each of the nodes.
  68.                     while (!reader.EOF)
  69.                     {
  70.                         if (reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.Attribute)
  71.                         {
  72.                             XElement el = XElement.ReadFrom(reader) as XElement;
  73.                             RemoveNamespaces(el);
  74.  
  75.                             var elements = (IEnumerable<object>)el.XPathEvaluate(xpath);
  76.  
  77.                             foreach (var e in elements)
  78.                             {
  79.                                 string key = string.Empty;
  80.  
  81.                                 if (e is XAttribute)
  82.                                 {
  83.                                     key = ((XAttribute)e).Value;
  84.                                     //Console.WriteLine(key);
  85.                                    
  86.                                     isFound = true;
  87.                                     isFoundInFile = true;
  88.                                 }
  89.                                 if (e is XElement)
  90.                                 {
  91.                                     key = ((XElement)e).Value;
  92.                                     //Console.WriteLine(key);
  93.  
  94.                                     isFound = true;
  95.                                     isFoundInFile = true;
  96.                                 }
  97.  
  98.                                 if (isFound)
  99.                                 {
  100.                                     if (dict.ContainsKey(key))
  101.                                     {
  102.                                         dict[key]++;
  103.                                     }
  104.                                     else
  105.                                     {
  106.                                         dict.Add(key, 1);
  107.                                     }
  108.                                 }
  109.  
  110.                                 isFound = false;
  111.                             }
  112.                         }
  113.                         else
  114.                             reader.Read();
  115.                     }
  116.                 }
  117.  
  118.                 var nakey = "N/A";
  119.                 if (!isFoundInFile)
  120.                 {
  121.                     if (dict.ContainsKey(nakey))
  122.                     {
  123.                         dict[nakey]++;
  124.                     }
  125.                     else
  126.                     {
  127.                         dict.Add(nakey, 1);
  128.                     }
  129.                 }
  130.             }
  131.  
  132.             var ordered = dict.OrderByDescending(x => x.Value);
  133.  
  134.             Console.WriteLine();
  135.             PrintEnumerable(ordered);
  136.             //PrintInFileEnumerable(ordered);
  137.             Console.WriteLine();
  138.  
  139.             Console.WriteLine("Закончили");
  140.             Console.ReadLine();
  141.         }
  142.  
  143.         private static void RemoveNamespaces(XElement document)
  144.         {
  145.             var elements = document.Descendants();
  146.             elements.Attributes().Where(a => a.IsNamespaceDeclaration).Remove();
  147.             foreach (var element in elements)
  148.             {
  149.                 element.Name = element.Name.LocalName;
  150.  
  151.                 var strippedAttributes =
  152.                     from originalAttribute in element.Attributes().ToArray()
  153.                     select (object)new XAttribute(originalAttribute.Name.LocalName, originalAttribute.Value);
  154.  
  155.                 //Note that this also strips the attributes' line number information
  156.                 element.ReplaceAttributes(strippedAttributes.ToArray());
  157.             }
  158.         }
  159.  
  160.         private static void PrintDictionary(Dictionary<string, int> dict)
  161.         {
  162.             foreach (var kvp in dict)
  163.             {
  164.                 Console.WriteLine($"Key: {kvp.Key}, value: {kvp.Value}");
  165.             }
  166.         }
  167.  
  168.         private static void PrintEnumerable(IOrderedEnumerable<KeyValuePair<string, int>> keyValuePairs)
  169.         {
  170.             foreach (var kvp in keyValuePairs)
  171.             {
  172.                 Console.WriteLine($"{kvp.Key}, {kvp.Value}");
  173.             }
  174.         }
  175.  
  176.         private static void PrintInFileEnumerable(IOrderedEnumerable<KeyValuePair<string, int>> keyValuePairs)
  177.         {
  178.             using (StreamWriter sw = new StreamWriter(@"D:\Downloads\info.txt", false, Encoding.Default))
  179.             {
  180.                 foreach (var kvp in keyValuePairs)
  181.                 {
  182.                     sw.WriteLine($"{kvp.Key}, {kvp.Value}");
  183.                 }
  184.             }
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement