How to search in a XML file

XML files consist of tags that encapsulate and organize information within them. The .NET technology ecosystem extensively supports XML as a file format, recognizing its versatility and wide adoption. Furthermore, the Dataset component in ADO.NET uses XML as its internal storage format, underscoring the key role XML plays in facilitating efficient data management and storage within the ADO.NET framework. This integration of XML as a foundational format within both the .NET technology and ADO.NET underscores its significance and highlights its efficacy in representing and manipulating structured data.

XmlReader

The following C# source code illustrates a practical example of searching for an item within an XML file using a Dataset. In this case, the Dataset utilizes an XmlReader to read the contents of the XML file. The XmlReader is employed to locate the XML file, serving as an argument for the Dataset. Using the Dataset, a search operation is conducted to find the product named "Product2" within the "Product.XML" file. This search is facilitated by employing a DataView, which allows for efficient filtering and querying of the Dataset's contents.

Full Source C#
using System; using System.Data; using System.Windows.Forms; using System.Xml; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { XmlReader xmlFile ; xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings()); DataSet ds = new DataSet(); DataView dv ; ds.ReadXml(xmlFile); dv = new DataView(ds.Tables[0]); dv.Sort = "Product_Name"; int index = dv.Find("Product2"); if (index == -1) { MessageBox.Show ("Item Not Found"); } else { MessageBox.Show(dv[index]["Product_Name"].ToString() + " " + dv[index]["Product_Price"].ToString()); } } } }

Click here to download the input file product.xml : product.xml