How To Read An Xml File In C#?

image

Here are a few ways to read an XML file in C#:

Use the XmlDocument class:

XmlDocument doc = new XmlDocument(); doc.Load("file.xml");

Use LINQ to XML:

XDocument doc = XDocument.Load("file.xml");

Use the XmlReader class:

XmlReader reader = XmlReader.Create("file.xml"); while (reader.Read()) { // process XML }

Use the XPathDocument class:

XPathDocument doc = new XPathDocument("file.xml"); XPathNavigator nav = doc.CreateNavigator();

Use the XmlSerializer class to deserialize into objects:

XmlSerializer serializer = new XmlSerializer(typeof(MyClass)); using (StreamReader reader = new StreamReader("file.xml")) { MyClass obj = (MyClass)serializer.Deserialize(reader); }

Related tutorial videos:

C# Tutorial Reading an XML file


C# How To Read Data From XML File Part 3 (720P High Quality)