using System; using System.IO; using System.Xml; using System.Xml.Schema; /* Given: we expect to see: root Bob Ted Sally but what we get is: root Bob How did we get a #comment? Ted Sally */ public class Program { public static void Main(string[] args) { XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; Stream stream = File.OpenRead("Test1.xml"); XmlReader reader = XmlReader.Create(stream, settings); XmlDocument xml = new XmlDocument(); xml.Load(reader); DoElement(xml.FirstChild, ""); } private static void DoElement(XmlNode element, string indent) { DoPrint(element, indent); foreach (XmlNode child in element.ChildNodes) { DoElement(child, indent + " "); } } private static void DoPrint(XmlNode element, string indent) { if (element.Name == "Customers" || element.Name == "Customer") { string name = element.Attributes["name"].Value; Console.WriteLine("{0}{1}", indent, name); } else Console.WriteLine("{0}How did we get a {1}?", indent, element.Name); } }