首页 > 代码库 > C# XmlTextWriter和XmlTextReader 读写XML文件
C# XmlTextWriter和XmlTextReader 读写XML文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace Xml { class Program { static void Main(string[] args) { // XmlTextWriter 写文件 XmlTextWriter writeXml = new XmlTextWriter(@"c:\MyXml.xml",Encoding.UTF8); writeXml.WriteStartDocument(false); writeXml.WriteStartElement("NetWork"); writeXml.WriteComment("网络配置信息"); writeXml.WriteStartElement("configration"); writeXml.WriteElementString("IpAddress","192.168.2.168"); writeXml.WriteElementString("Netmask", "255.255.255.0"); writeXml.WriteElementString("Gateway", "202.103.24.68"); writeXml.WriteEndElement(); writeXml.WriteEndElement(); writeXml.Flush(); writeXml.Close(); // XmlTextReader 读文件 XmlTextReader readerXml = new XmlTextReader(@"c:\MyXml.xml"); while(readerXml.Read()) { if(readerXml.NodeType == XmlNodeType.Element) { if(readerXml.Name == "IpAddress") { Console.WriteLine(readerXml.ReadElementString().Trim()); } if (readerXml.Name == "Netmask") { Console.WriteLine(readerXml.ReadElementString().Trim()); } if (readerXml.Name == "Gateway") { Console.WriteLine(readerXml.ReadElementString().Trim()); } } } Console.ReadKey(); } } }Xml文件内容:
控制台运行结果:
参考博客:
http://www.cnblogs.com/a1656344531/archive/2012/11/28/2792863.html
C# XmlTextWriter和XmlTextReader 读写XML文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。