Click here to Skip to main content
15,895,606 members
Articles / Programming Languages / C# 4.0

Quick XML Reader

Rate me:
Please Sign up or sign in to vote.
4.78/5 (18 votes)
23 Feb 2011CPOL4 min read 85.3K   14.9K   72  
A quick XML interpreter for large XML files.
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 WeifenLuo.WinFormsUI.Docking;
using System.Threading;

namespace Trezorix.Qxr.Forms
{
	public partial class SearchResults : WeifenLuo.WinFormsUI.Docking.DockContent
	{
		public SearchResults()
		{
			InitializeComponent();
		}

		private Document m_currentSearchingDocument = null;

		private void SearchResults_Load(object sender, EventArgs e)
		{
			tbSearchText.Focus();
			tbSearchText.SelectAll();
			DockPanel.ActiveDocumentChanged += new EventHandler(DockPanel_ActiveDocumentChanged);
			FindSearchTarget();
		}

		private void DockPanel_ActiveDocumentChanged(object sender, EventArgs e)
		{
			FindSearchTarget();
		}

		private void RegisterEvents()
		{
			m_currentSearchingDocument.XmlReadStream.Search.SearchComplete += new Streamer.QuickXmlSearch.SearchCompleteEventHandler(Search_SearchComplete);
			m_currentSearchingDocument.XmlReadStream.Search.FoundItem += new Streamer.QuickXmlSearch.FoundItemEventHandler(Search_FoundItem);
		}
		private void UnregisterEvents()
		{
			m_currentSearchingDocument.XmlReadStream.Search.SearchComplete -= Search_SearchComplete;
			m_currentSearchingDocument.XmlReadStream.Search.FoundItem -= Search_FoundItem;
		}

		private void Search_FoundItem(object sender, Streamer.EventArguments.FoundItemEventArgs e)
		{
			AddFoundItem(e.Text, e.Line);
		}

		private void Search_SearchComplete(object sender, EventArgs e)
		{
			HideSearchProgress();
		}



		private void FindSearchTarget()
		{
			IDockContent activeDocument = DockPanel.ActiveDocument;
			if ((activeDocument != null) && (activeDocument is Document))
			{

				if (m_currentSearchingDocument != null)
				{
					UnregisterEvents();
				}

				m_currentSearchingDocument = activeDocument as Document;
				lstSearchResults.Items.Clear();
				tbSearchText.Text = "";
				if (m_currentSearchingDocument.XmlReadStream != null)
				{

					tbSearchText.Text = m_currentSearchingDocument.XmlReadStream.Search.SearchPhrase;
					lstSearchResults.BeginUpdate();
					foreach (KeyValuePair<int, string> searchResult in m_currentSearchingDocument.XmlReadStream.Search.SearchResults)
					{
						ListViewItem itmResult = new ListViewItem();
						itmResult.Text = searchResult.Value;
						itmResult.Tag = searchResult.Key;
						itmResult.SubItems.Add(searchResult.Key.ToString());
						lstSearchResults.Items.Add(itmResult);
					}
					lstSearchResults.EndUpdate();

					if (m_currentSearchingDocument.XmlReadStream.Search.IsSearching)
						DisplaySearchProgress();
					else
						HideSearchProgress();
					RegisterEvents();
				}
			}
		}
		private void btnFind_Click(object sender, EventArgs e)
		{
			SearchForText();
		}
		private void tbSearchText_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
				SearchForText();
		}

		private void SearchForText()
		{
			DisplaySearchProgress();
			lstSearchResults.Items.Clear();
			UnregisterEvents();
			RegisterEvents();
			m_currentSearchingDocument.XmlReadStream.SearchPhrase(tbSearchText.Text);
		}



		private void XmlReadStream_FoundItem(object sender, Streamer.EventArguments.FoundItemEventArgs e)
		{
			AddFoundItem(e.Text, e.Line);
		}


		private delegate void OnAddFoundItem(string Text, int LineNumber);
		private void AddFoundItem(string Text, int LineNumber)
		{
			if (InvokeRequired)
			{
				Invoke(new OnAddFoundItem(AddFoundItem), new object[] { Text, LineNumber });
			}
			else
			{
				ListViewItem itmFound = new ListViewItem();
				itmFound.Text = Text;
				itmFound.SubItems.Add(LineNumber.ToString());
				itmFound.Tag = LineNumber;
				lstSearchResults.Items.Add(itmFound);
			}
		}

		private void DisplaySearchProgress()
		{
			prgSearching.Style = ProgressBarStyle.Marquee;
			prgSearching.Visible = true;
		}
		private delegate void OnHideSearchProgress();
		private void HideSearchProgress()
		{
			if (InvokeRequired)
			{
				Invoke(new OnHideSearchProgress(HideSearchProgress));
			}
			else
			{
				prgSearching.Visible = false;
				prgSearching.Style = ProgressBarStyle.Blocks;
			}
		}

		private void tbSearchText_TextChanged(object sender, EventArgs e)
		{
			btnFind.Enabled = ((tbSearchText.TextLength > 0) && (tbSearchText.Text.ToLower() != "search text"));
		}

		private void SearchResults_Resize(object sender, EventArgs e)
		{
			lstSearchResults.Columns[0].Width = Width - lstSearchResults.Columns[1].Width - 24;
		}

		private void lstSearchResults_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (m_currentSearchingDocument != null)
			{
				if (lstSearchResults.SelectedItems.Count > 0)
				{
					int scrollLine = int.Parse(lstSearchResults.SelectedItems[0].Tag.ToString());
					scrollLine = (scrollLine > 5) ? scrollLine - 5 : 0;
					m_currentSearchingDocument.Activate();
					m_currentSearchingDocument.Scroll(scrollLine);
					m_currentSearchingDocument.MarkSearchedText();
				}
			}
		}

		private void SearchResults_FormClosing(object sender, FormClosingEventArgs e)
		{
			DockPanel.ActiveDocumentChanged -= DockPanel_ActiveDocumentChanged;

			HideSearchProgress();
			tbSearchText.Text = null;

			if (m_currentSearchingDocument != null)
				UnregisterEvents();
		}




	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect http://4dotnet.nl
Netherlands Netherlands
I'm developing software for over two decades. I'm working as a cloud solution architect and a team lead at 4DotNet in The Netherlands.

In my current position, I love to help customers with their journey to the cloud. I like to create highly performant software and to help team members to a higher level of software development.

My focus is on the Microsoft development stack, mainly C# and the Microsoft Azure Cloud. I also have a strong affinity with Angular. As a result of my contributions to the community, I received the Microsoft MVP Award for Microsoft Azure.

Comments and Discussions