此示例使用 XmlTextReader 类从示例文件中提取元素名称和文本字符串,并将该信息存储在字符串变量中。
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader("c:\\IntroToVCS.xml"); string contents = ""; while (reader.Read()) { reader.MoveToContent(); if (reader.NodeType == System.Xml.XmlNodeType.Element) contents += "<"+reader.Name + ">\n"; if (reader.NodeType == System.Xml.XmlNodeType.Text) contents += reader.Value + "\n"; } System.Console.Write(contents);