简体   繁体   中英

How to get input from textbox in C#?

First things first,I am very new to C# and programming all around. I have a standalone program that will read XML files in a certain location and convert them to plaintext files.

And I have a windows forms app that has a file directory and will display the chosen file in a textbox.

{
        Stream myStream;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if ((myStream = openFileDialog1.OpenFile())!= null)
            {
                string strfilename = openFileDialog1.FileName;
                string filetext = File.ReadAllText(strfilename);
                textBox1.Text = filetext;
            }
        }
    }

Below is a snippet of my conversion program.

string[] files = Directory.GetFiles("C:\\articles");
        foreach (string file in files)
        {
            List<string> translatedLines = new List<string>();
            string[] lines = File.ReadAllLines(file);
            foreach(string line in lines)
            {
                if (line.Contains("\"check\""))
                {
                    string pattern = "<[^>]+>";
                    string replacement = " ";
                    Regex rgx = new Regex(pattern);
                    string result = rgx.Replace(line, replacement);
                    translatedLines.Add(result);
                }
            }

How would I modify my program to take the input from the textbox and then perform its conversion? (And Yes I'm aware I have to combine the two programs.)

Use XDocument class to parse the XML formatted string to XML Document so that you can get values on each Nodes of the XML

XDocument xDoc = new XDocument(); 
xDoc = XDocument.Parse(filetext);

Now read the content:

var textValue = xDoc.Descendants("Response").First().Attribute("Status").Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM