How to filter in a XML file

XML, as a platform-independent language, offers the advantage of seamless interchangeability of information across different platforms and operating systems. When information is formatted in XML, it becomes universally accessible, enabling easy utilization across diverse platforms. Moreover, the .NET technology extensively supports XML as a file format, recognizing its wide adoption and versatility. Additionally, the Dataset component in ADO.NET leverages XML as its internal storage format, further emphasizing the crucial role XML plays in facilitating efficient data management and storage within the ADO.NET framework.

Filter in a XML file

In this scenario, our objective is to filter the content of an XML file and store the filtered result in a newly created XML file using C#. The source XML file, named "Product.XML," contains a field called "Product_Price." We apply a search criterion where the Product_Price is greater than or equal to 3000, and the filtered result is stored in a newly generated XML file called "Result.XML." This process allows for the extraction and isolation of specific data based on the defined criteria, resulting in a refined XML file with the desired information.

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], "Product_price > = 3000", "Product_Name", DataViewRowState.CurrentRows); dv.ToTable().WriteXml("Result.xml"); MessageBox.Show ("Done"); } } }

You can see the filter result in Result.xml file

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