C# XmlTextWriter和XmlTextReader 读写XML文件_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# XmlTextWriter和XmlTextReader 读写XML文件

C# XmlTextWriter和XmlTextReader 读写XML文件

 2015/2/2 16:08:41  GC2013  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Xml;namespaceXml{classProgram{staticvoidMain(string[]args){//XmlTextWriter写文件XmlTextWriterwriteXml=newXmlTextWriter(@c:MyXml.xml,Encoding.UTF8);writeXml
  • 标签:C# 文件 XML文件 XML
class="java plain" style="background: none !important; margin: 0px !important; padding: 0px !important; outline: 0px !important; border-radius: 0px !important; border: 0px currentColor !important; left: auto !important; top: auto !important; width: auto !important; height: auto !important; text-align: left !important; right: auto !important; bottom: auto !important; color: black !important; line-height: 1.1em !important; overflow: visible !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; font-style: normal !important; font-weight: normal !important; vertical-align: baseline !important; float: none !important; position: static !important; min-height: inherit !important; box-sizing: content-box !important;">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文件内容:

\

控制台运行结果:

\

发表评论
用户名: 匿名