简体   繁体   English

如何从C#中的文本框获取输入?

[英]How to get input from textbox in C#?

First things first,I am very new to C# and programming all around. 首先,我对C#和编程非常陌生。 I have a standalone program that will read XML files in a certain location and convert them to plaintext files. 我有一个独立的程序,它将在特定位置读取XML文件并将其转换为纯文本文件。

And I have a windows forms app that has a file directory and will display the chosen file in a textbox. 我有一个Windows窗体应用程序,该应用程序具有文件目录,并将在文本框中显示所选文件。

{
        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类将XML格式的字符串解析为XML Document,以便可以获取XML的每个节点上的值

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

Now read the content: 现在阅读内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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