Advertisement
stanevplamen

02.07.10.XmlFileExtract

Aug 12th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. class XmlFileExtract
  7. {
  8.     static void Main()
  9.     {
  10.         // 1. extracting text from xml file
  11.         string overallString = TextExtract();
  12.  
  13.         // 2. lining up the answer (on separate lines) and export to e file
  14.         LoadResultFile(overallString);
  15.     }
  16.  
  17.     static string RemoveBetween(string s, char begin, char end)
  18.     {
  19.         Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end));
  20.         string output = regex.Replace(s, string.Empty + "\r\n");
  21.         return output;
  22.     }
  23.  
  24.     static string TextExtract()
  25.     {
  26.         string textFile = @"..\..\TestFile.xml";
  27.         StreamReader streamReader = new StreamReader(textFile);
  28.         string fileContents = null;
  29.  
  30.         using (streamReader)
  31.         {
  32.             fileContents = streamReader.ReadToEnd();
  33.         }
  34.         string removeBrackets = RemoveBetween(fileContents, '<', '>');
  35.         return removeBrackets;
  36.     }
  37.  
  38.     static void LoadResultFile(string outputs)
  39.     {
  40.         string fileName = @"..\..\ResultText.txt";
  41.         StreamWriter streamWriter = new StreamWriter(fileName);
  42.         using (streamWriter)
  43.         {
  44.             streamWriter.Write(outputs);
  45.         }
  46.  
  47.         StreamReader streamReader = new StreamReader(fileName);
  48.         StringBuilder MyStringBuilder = new StringBuilder();
  49.         using (streamReader)
  50.         {
  51.             int lineNumber = 1;
  52.             string line = streamReader.ReadLine();
  53.             while (line != null)
  54.             {
  55.                 if ((line) != string.Empty)
  56.                 {
  57.                     MyStringBuilder.Append(line);
  58.                     MyStringBuilder.AppendLine();
  59.                 }
  60.                 line = streamReader.ReadLine();
  61.                 lineNumber++;
  62.             }
  63.         }
  64.  
  65.         string overallString = MyStringBuilder.ToString();
  66.         StreamWriter streamWriter2 = new StreamWriter(fileName);
  67.         using (streamWriter2)
  68.         {
  69.             streamWriter2.Write(overallString);
  70.         }
  71.         Console.WriteLine(overallString);
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement