string xmlString = ""; XmlTextReader reader = new XmlTextReader(new StringReader(xmlString)); reader.ReadToFollowing("title"); string title = reader.ReadString(); Console.WriteLine(title); C# Basics
string xmlFile = "books.xml"; XmlTextReader reader = new XmlTextReader(xmlFile); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "title") { string title = reader.ReadString(); Console.WriteLine(title); } }In this example, we create an XmlTextReader object from an XML file. We then use a while loop to read through the entire XML document. When we encounter an "title" element, we use ReadString method to read its value and print it to the console. Both examples demonstrate how ReadString can be used to read the values of XML elements. The System.Xml namespace is used to access the XmlTextReader class.