How to read XML files in C#? Please provide a sample.
How to read XML files in C#? Please provide a sample.
18327-Sep-2023
Updated on 28-Sep-2023
Home / DeveloperSection / Forums / How to read XML files in C#? Please provide a sample.
How to read XML files in C#? Please provide a sample.
Aryan Kumar
28-Sep-2023In C#, you can read XML files using the System.Xml namespace, which provides classes like XmlDocument, XmlTextReader, and XDocument for parsing and working with XML data. Here, I'll provide a sample using both XmlDocument and XDocument to read an XML file:
Sample XML File (example.xml):
Reading XML using XmlDocument:
Reading XML using XDocument (LINQ to XML):
Both XmlDocument and XDocument provide methods for loading and querying XML data. XDocument is part of LINQ to XML and is often preferred for its simplicity and integration with LINQ. Choose the one that best fits your needs and coding style.