Guest User

XML Reader

a guest
Aug 12th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.IO;
  8. using System.Windows.Forms;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Xml.Linq;
  18. using System.Text.RegularExpressions;
  19.  
  20. namespace WWI_Auslesen
  21. {
  22.     public partial class MainWindow : Window
  23.     {
  24.  
  25.         public MainWindow()
  26.         {
  27.             InitializeComponent();
  28.  
  29.         }
  30.  
  31.         public RichTextBoxScrollBars ScrollBars { get; set; }
  32.         private void Choose_File(object sender, RoutedEventArgs e)
  33.         {
  34.             OpenFileDialog fileDialog = new OpenFileDialog();
  35.             StringBuilder sb = new StringBuilder();
  36.  
  37.             fileDialog.Multiselect = true;
  38.             fileDialog.Filter = "WWI Files|*.wwi|XML Files|*.xml";
  39.             fileDialog.DefaultExt = "*.wwi";
  40.  
  41.             DialogResult result = fileDialog.ShowDialog();
  42.  
  43.             if (result == System.Windows.Forms.DialogResult.OK)
  44.             {
  45.                 string[] xmlTextLines = File.ReadAllLines(fileDialog.FileName);
  46.                 foreach (var line in xmlTextLines)
  47.                 {
  48.                     if (string.IsNullOrWhiteSpace(line))
  49.                         continue;
  50.                     string timeIdentifier = line.Contains("S:<") ? "S:<" : "R:<";
  51.                     string timeString = line.Substring(0, line.IndexOf(timeIdentifier));
  52.                     string xmlString = line.Substring(line.IndexOf(timeIdentifier) + 2);
  53.                     xmlString = XDocument.Parse(xmlString).ToString();
  54.                     sb.Append(timeString);
  55.                     sb.AppendLine(timeIdentifier.Remove(timeIdentifier.Length - 1, 1));
  56.                     sb.AppendLine(xmlString);
  57.                     sb.AppendLine();
  58.                     RichTextBox1.AppendText(sb.ToString());
  59.  
  60.                 }
  61.             }
  62.         }
  63.  
  64.         private void richtextBox1_PreviewDrop(object sender, System.Windows.DragEventArgs e)
  65.         {
  66.             StringBuilder sb = new StringBuilder();
  67.             string[] fileloadup = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
  68.             string[] xmlTextLines = File.ReadAllLines(fileloadup[0]);
  69.             foreach (var line in xmlTextLines)
  70.             {
  71.                 if (string.IsNullOrWhiteSpace(line))
  72.                     continue;
  73.                 string timeIdentifier = line.Contains("S:<") ? "S:<" : "R:<";
  74.                 string timeString = line.Substring(0, line.IndexOf(timeIdentifier));
  75.                 string xmlString = line.Substring(line.IndexOf(timeIdentifier) + 2);
  76.                 xmlString = XDocument.Parse(xmlString).ToString();
  77.                 sb.Append(timeString);
  78.                 sb.AppendLine(timeIdentifier.Remove(timeIdentifier.Length - 1, 1));
  79.                 sb.AppendLine(xmlString);
  80.                 sb.AppendLine();
  81.                 RichTextBox1.AppendText(sb.ToString());
  82.             }
  83.             e.Handled = true;
  84.         }
  85.  
  86.         private void richtextBox1_PreviewDragOver(object sender, System.Windows.DragEventArgs e)
  87.         {
  88.             e.Effects = System.Windows.DragDropEffects.All;
  89.             e.Handled = true;
  90.         }
  91.         private void Cancel(object sender, RoutedEventArgs e)
  92.         {
  93.             this.Close();
  94.         }
  95.  
  96.         private void Button_Search(object sender, RoutedEventArgs e)
  97.         {
  98.             TextRange textRange = new TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd);
  99.             textRange.ClearAllProperties();
  100.             lbl_Status.Content = "";
  101.             string textBoxText = textRange.Text;
  102.             string searchText = userInput.Text;
  103.  
  104.             if (string.IsNullOrWhiteSpace(textBoxText) || string.IsNullOrWhiteSpace(searchText))
  105.             {
  106.                 lbl_Status.Content = "Bitte erst eine Datei wählen!";
  107.             }
  108.             else
  109.             {
  110.                 Regex regex = new Regex(searchText);
  111.                 int count_MatchFound = Regex.Matches(textBoxText, regex.ToString()).Count;
  112.  
  113.                 for (TextPointer startPointer = RichTextBox1.Document.ContentStart;
  114.                                 startPointer.CompareTo(RichTextBox1.Document.ContentEnd) <= 0;
  115.                                     startPointer = startPointer.GetNextContextPosition(LogicalDirection.Forward))
  116.                 {
  117.                     if (startPointer.CompareTo(RichTextBox1.Document.ContentEnd) == 0)
  118.                     {
  119.                         break;
  120.                     }
  121.                     string parsedString = startPointer.GetTextInRun(LogicalDirection.Forward);
  122.                     int indexOfParseString = parsedString.IndexOf(searchText);
  123.                     if (indexOfParseString >= 0)
  124.                     {
  125.                         startPointer = startPointer.GetPositionAtOffset(indexOfParseString);
  126.  
  127.                         if (startPointer != null)
  128.                         {
  129.                             TextPointer nextPointer = startPointer.GetPositionAtOffset(searchText.Length);
  130.                             TextRange searchedTextRange = new TextRange(startPointer, nextPointer);
  131.                             searchedTextRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
  132.                         }
  133.                     }
  134.                 }
  135.                 if (count_MatchFound > 0)
  136.                 {
  137.                     lbl_Status.Content = "Erzielte Treffer: " + count_MatchFound;
  138.                 }
  139.                 else
  140.                 {
  141.                     lbl_Status.Content = "Kein Treffer erzielt!";
  142.                 }
  143.             }
  144.         }
  145.  
  146.         private void WatermarkedTxt_GotFocus(object sender, RoutedEventArgs e)
  147.         {
  148.             watermarkedTxt.Visibility = System.Windows.Visibility.Collapsed;
  149.             userInput.Visibility = System.Windows.Visibility.Visible;
  150.             userInput.Focus();
  151.         }
  152.  
  153.         private void UserInput_LostFocus(object sender, RoutedEventArgs e)
  154.         {
  155.             if (string.IsNullOrEmpty(userInput.Text))
  156.             {
  157.                 userInput.Visibility = System.Windows.Visibility.Collapsed;
  158.                 watermarkedTxt.Visibility = System.Windows.Visibility.Visible;
  159.             }
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment