C # XmlTextWriter and XmlTextReader read and write XML files,
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 writes a file XmlTextWriter writeXml = new XmlTextWriter( @c :MyXml.xml,Encoding.UTF8); writeXml.WriteStartDocument( false ); writeXml.WriteStartElement(NetWork); WriteXml. WriteComment (network configuration information ); 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 reads the file 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 file content:
Console running result: