Удалить все не нужные слова в listbox - C#
Формулировка задачи:
Ребят, есть проблема. Не могу удалить некоторые слова в listboxe.
У меня есть текст в txt документе
Вывод его в listbox я сделал. Теперь мне нужно по нажатию кнопки сделать так чтобы осталась только ссылка "http://www.test.html"
Таких ссылок много. Не знаю как реализовать. Пробовал много чего, но не закомпилило.
Все действия в button1
Мой код:
<url>
<loc>http://www.test.html</loc>
<lastmod>2013-11-25</lastmod>
<changefreq>monthly</changefreq>
</url>
<url>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string pathToFile = "base.txt";
// Считываем строки в массив
string[] allLines = File.ReadAllLines(pathToFile, Encoding.GetEncoding(1251));
// Добавляем каждую строку
foreach (string line in allLines)
listBox1.Items.Add(line);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
private void button3_Click(object sender, EventArgs e)
{
List<string> list = new List<string>();
for (int i = 0; i < listBox1.Items.Count; i++)
list.Add(listBox1.Items[i].ToString());
System.IO.File.WriteAllLines(@"base.txt", list.ToArray(), Encoding.Default);
}
private void button4_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("chrome.exe", listBox1.SelectedItem.ToString());
}
}
}Решение задачи: «Удалить все не нужные слова в listbox»
textual
Листинг программы
private void button1_Click(object sender, EventArgs e)
{
string myXmlfile = File.ReadAllText(@"base.txt");
XmlDocument doc = new XmlDocument();
XmlDocumentFragment fragment = doc.CreateDocumentFragment();
fragment.InnerXml = myXmlfile;
var urls = fragment.SelectNodes("//url/loc")
.Cast<XmlNode>()
.Select(node => node.InnerText)
.ToArray();
listBox1.Items.Clear();
listBox1.Items.AddRange(urls);
}