我在Form1中有这个代码。我正在搜索xml文件。当我发现他们使用listBox1选择索引更改事件时,我想在lixtBox中选择项目时这样做,它会将其视为一个文件将解析它的内容并显示解析的内容。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System.Collections;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DirectoryInfo dirinf = new DirectoryInfo(@"C:\");
List<FileSystemInfo> fsi = new List<FileSystemInfo>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
button1.Enabled = false;
}
private void ParseAndDisplayXml(string filename)
{
XDocument document = XDocument.Load(filename);
var list = document.Root.Elements("Message")
.Select(
e => new
{
Date = e.Attribute("Date").Value.ToString(),
Time = e.Attribute("Time").Value.ToString(),
Text = e.Element("Text").Value.ToString()
}
);
string result="";
foreach (var item in list)
{
result += string.Format("Date--{0},Time--{1},Text--{2}", item.Date, item.Time, item.Text + Environment.NewLine);
}
}
public void Search(string strExtension,
DirectoryInfo di,
List<FileSystemInfo> pResult)
{
try
{
foreach (FileInfo fi in di.GetFiles())
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => label2.Text = fi.Name));
}
if (fi.Name == "MessageLog.xsl")
{
foreach (FileInfo fii in di.GetFiles())
{
if (fii.Extension == strExtension)
pResult.Add(fii);
}
if (InvokeRequired)
{
BeginInvoke(new Action(() => label4.Text = pResult.Count.ToString() + Environment.NewLine));
}
}
}
foreach (DirectoryInfo diChild in di.GetDirectories())
Search(strExtension, diChild, pResult);
}
catch (Exception e)
{
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Search(".xml", dirinf, fsi);
backgroundWorker1.ReportProgress(100);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
for (int i = 0; i < fsi.Count; i++)
{
listBox1.Items.Add(fsi[i].Name + Environment.NewLine);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label2.Text = listBox1.SelectedItem.ToString();
}
}
}
我从C开始搜索:\ 然后,当搜索结束时,我将它找到的项目添加到listBox1。
例如现在我的listBox1中有4个文件:
danny.xml adi.xml sharon.xml yoval.xml
在selectedindexchanged i added选项中,用户可以在项目之间移动。
现在我要做的是当用户在listBox中为例如index [1]选择一些索引时,只有当他点击键盘输入或点击鼠标左键单击它才会调用/使用函数:ParseAndDisplayXML
然后它将解析需要转换为文件的所选索引,因此在backgroundWorker1_RunWorkerCompleted事件中,我将文件作为项目进入listBox,但仅限于文件的名称。如果我做.FullName而不是.Name它也是用目录添加文件名。
因此我需要以某种方式获取已完成事件中文件的FullName,然后选择其中一个FullName项目进行解析并将其显示在listBox中。
解析函数应该从xml文件中获取特定内容,然后单独检查此函数。
问题是如何让用户通过点击/键输入选择索引以及如何解析和显示它?
答案 0 :(得分:0)
向列表框添加内容时。 它需要一个对象,并将文本设置为object.ToString()
e.g。
MyListBox.Add(100);
框100并显示“100”
无法找到FileSystemInfo的ToString()方法是否已被覆盖,但首先要尝试的是
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
// newline is unnecesary and you should be using foreach
foreach(FileSystemInfo f in fsi)
{
listBox1.Items.Add(f);
}
}
// display full name of file
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label2.Text = ((FileSystemInfo)listBox1.SelectedItem).Fullname;
}
如果FileSystemInfo.ToString()没有返回Name,有几种方法可以解决这个问题。 如果您不想继续使用FileSystemInfo实例,我们也可以处理它。