Stream based XML file operation notes in C
- 2021-06-28 13:44:12
- OfStack
System.Xml.XmlReader and System.Xml.XmlWriters are two abstract classes. XmlReader provides a fast, non-cached, feed-only reader for XML data. XmlWriter represents a writer that provides a fast, non-cached, forward-only way to generate stream files containing XML data.
XmlReader is similar to the SAX reader, but the former is the extract mode (read-only cursor) and the latter is the push mode (push events to the application).
The advantages of XmlReader are:
1. Simplify state management
2. There can be multiple input streams
3. Extraction mode can be used as the basis of push mode, but not vice versa.
4. Provide a buffer where strings can be written directly to avoid duplication of extra bytes.
5. Optionally, XML streams can be processed in a way that is preset for XML streams.
The code logic below is simple and does not cause exceptions, so exception handling is not added, but is used as a test.
// ReadXml.cs
// XmlReader Example
using System;
using System.Xml;
class Test
{
public static void Main(string[] args)
{
string path = @"G:\ data \C# object-oriented programming \C# object-oriented programming \CSharp\Chapter17\books.xml";
XmlReader reader = null;
try
{
// Establish XmlReaderSettings object
XmlReaderSettings settings = new XmlReaderSettings();
// Set up XmlReaderSettings Object Properties
settings.ProhibitDtd = false;
// Use XmlReaderSettings Object to create XmlReader object
reader = XmlReader.Create(path, settings);
ReadXml(reader);
reader.Close();
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (reader != null)
reader.Close();
}
}
private static void ReadXml(XmlReader reader)
{
try
{
// Parse the file and display each 1 Nodes
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.IsEmptyElement) // Empty element?
{
Console.WriteLine("<{0}/>", reader.Name);
}
else
{
Console.Write("<{0}", reader.Name);
if (reader.HasAttributes) // Attributes?
{
while (reader.MoveToNextAttribute())
{
Console.Write(" {0}=\"{1}\"", reader.Name, reader.Value);
}
}
Console.WriteLine(">", reader.Name);
}
break;
case XmlNodeType.Text:
Console.WriteLine(reader.Value);
break;
case XmlNodeType.CDATA:
Console.WriteLine("<![CDATA[{0}]]>", reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.WriteLine("<?{0} {1}?>", reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
Console.WriteLine("<!--{0}-->", reader.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.WriteLine("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.WriteLine("<!DOCTYPE {0} [{1}]>", reader.Name, reader.Value);
break;
case XmlNodeType.EntityReference:
Console.WriteLine(reader.Name);
break;
case XmlNodeType.EndElement:
Console.WriteLine("</{0}>", reader.Name);
break;
}
}
}
catch (XmlException ex)
{
Console.WriteLine(ex.Message);
}
}
}
XmlWriters This writer provides a fast, non-cached, forward-only way to generate streaming files containing XML data, with essentially the same benefits as the former.
Note: With XmlReader, elements and attributes are not read until the Close method is called.
XMLWriter
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string path=@"C:\Users\dell\Desktop\test.xml";
XmlWriter writer=null;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars=("");
writer = XmlWriter.Create(path, settings);
WriteXml(writer);
writer.Close();
}
private static void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("books");
WriteChildNode(writer, " The Dream of Red Mansion ", " Cao Xueqin ", "25");
WriteChildNode(writer, "3 National Romance ", " Luo Guanzhong ", "20");
WriteChildNode(writer, " Water Margin ", " Shi Nai'an ", "18");
WriteChildNode(writer, " Liao Zhai Zhiyi ", " Pu Songling ", "16");
writer.WriteEndElement();
}
private static void WriteChildNode(XmlWriter writer, string title, string author, string price)
{
writer.WriteStartElement("book");
writer.WriteStartElement("title");
writer.WriteValue(title);
writer.WriteEndElement();
writer.WriteStartElement("author");
writer.WriteValue(author);
writer.WriteEndElement();
writer.WriteStartElement("price");
writer.WriteValue(price);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
}
The basic usage is simple, that is, to output the front and back switch labels, mainly using the three methods WriteStartElement () writer.WriteValue () WriteEndElement ()
Note: When the XmlWriter method outputs XML, elements and attributes are not written until the Close method is called.