[英]How to get input from textbox in C#?
首先,我對C#和編程非常陌生。 我有一個獨立的程序,它將在特定位置讀取XML文件並將其轉換為純文本文件。
我有一個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;
}
}
}
以下是我的轉換程序的摘要。
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);
}
}
如何修改程序以從文本框中獲取輸入,然后執行其轉換? (是的,我知道我必須組合兩個程序。)
使用XDocument類將XML格式的字符串解析為XML Document,以便可以獲取XML的每個節點上的值
XDocument xDoc = new XDocument();
xDoc = XDocument.Parse(filetext);
現在閱讀內容:
var textValue = xDoc.Descendants("Response").First().Attribute("Status").Value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.