2016-09-08 3 views
-1

Ich habe eine App erstellt, die einen Dateidialog öffnet, Sie können eine Datei öffnen und dann ihren Inhalt in einem Textfeld anzeigen. Wie auch immer, es gibt drei Textblöcke aus der Datei, die ich wirklich brauche. Ich muss in der Lage sein, diese Textblöcke nur zu "parsen". Ich habe Lesetexte verwendet. Allerdings nicht sicher, wie für jede Schleife sortiert werden soll. Jede mögliche Hilfe würde geschätzt:Wie können bestimmte Textzeilen aus einer Textdatei in einer Textbox angezeigt werden?

Mein Code

private void button1_Click(object sender, EventArgs e) 
{ 
    Stream myStream; 
    OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

    //Setting initial directory to the automated repository 
    openFileDialog1.InitialDirectory = "\\\\10.2.XXX.XX\\Repository\\AutomatedVersionRepository\\JMN Web Application\\"; 

    // Set filter options and filter index. 
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|Config files (*.config*)|*.config*"; 
    openFileDialog1.FilterIndex = 2; 
    openFileDialog1.Multiselect = true; 
    openFileDialog1.RestoreDirectory = true; 

    // Call the ShowDialog method to show the dialog box. 
    //bool? userClickedOK = openFileDialog1.ShowDialog() ==DialogResult.OK; 
    if (openFileDialog1.ShowDialog() ==System.Windows.Forms.DialogResult.OK) 
    { 
     if ((myStream = openFileDialog1.OpenFile()) !=null) 
     { 
      string strfilename = openFileDialog1.FileName; 
      string []filetext = File.ReadAllLines(strfilename); 
      foreach (string line in File.ReadLines(strfilename)) 
      { 
       //process the line 
      } 
      //richTextBox1.Text = filetext; 
     } 

Beispiel von Textblöcken:

<!---Interface URLS and settings--> 
<add key="IUFUploadPath" value="" /> 
<add key="JSDskAPIURL" value="http://api.documentation.com/16_4" /> 
<add key="PvAPIURL" value="http://devapi.documentation.com/16_4" /> 
<add key="FirstDataWebServiceURL" value="https://dev.SpectrumRetailNet.Com/PPSAPI" /> 
<add key="ReportServerUrl" value="https://reports64.documentation.com/9_2_0/FrameHost.aspx" /> 
<add key="HelpassAdminUrl" value="http://Helpppass.documentation.com/app/AdminConsole.aspx" /> 
<add key="XAIMAPIURL" value="//devquippe.documentation.com/ws.aspx" /> 
+0

Das sieht aus wie eine XML verwenden würde Sie sollten also XML-Klassen verwenden, was die Arbeit viel einfacher macht. – jdweng

Antwort

0

Dies ist der Code, den ich

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      Dictionary<string, string> settingsDict = new Dictionary<string, string>(); 
      XmlReaderSettings settings = new XmlReaderSettings(); 
      settings.ConformanceLevel = ConformanceLevel.Fragment; 
      XmlReader reader = XmlReader.Create(FILENAME, settings); 
      while (!reader.EOF) 
      { 
       if (reader.Name != "add") 
       { 
        reader.ReadToFollowing("add"); 
       } 
       if (!reader.EOF) 
       { 
        XElement add = (XElement)XElement.ReadFrom(reader); 
        settingsDict.Add((string)add.Attribute("key"), (string)add.Attribute("value")); 
       } 
      } 
     } 
    } 
} 
+0

Danke Jdweng Ich werde deinen Vorschlag versuchen! – Sonic1980

Verwandte Themen