Nevron Open Vision Documentation
Simple API for XML

The Simple API for XML (SAX) model provides fast, forward-only memory efficient way for reading and writing of XML data. It is very suitable for large XML files, because only the current XML token is kept in memory (and not the whole document as in the XML Document Object Model), which makes the process consume a negligible amount of memory no matter how big the processed XML document is. A drawback of the model is that, because it does not read the whole document at once, it does not provide any XML validation capabilities.

Reading XML Data

To read XML data using the SAX model, you can take advantage of the NXmlReader class. Call its static Create method passing the stream of XML data you want to read to it. Then you should call the Read method until it returns true and inspect each read node. The properties of interest are Name, Value and NodeType. The Name and Value properties have different meaning depending on the node type:

Node Type Name Value
StartTag The name of the tag. null
EndTag The name of the tag. null
Declaration The name of the declaration. null
Text null The text content of the text node.
Comment null The content of the comment.
CData null The content of the CData.


The following example demonstrates how to read a XML data stream with the Nevron XML reader:

NXmlReader Example
Copy Code
NXmlReader xmlReader = NXmlReader.Create(stream);
while (xmlReader.Read())
    Console.WriteLine("Node type: " + xmlReader.NodeType.ToString());
    Console.WriteLine("Name: " + xmlReader.Name);
    Console.WriteLine("Value: " + xmlReader.Value);
Writing XML Data

To write XML data using the SAX model, use the NXmlWriterClass. Call its static Create method passing the stream to write to and optionally an encoding (by default UTF-8 encoding is used). Then use the different Write methods to write various XML tokens to the output stream. The following example demonstrates how to write some XML data to a stream:

NXmlWriter Example
Copy Code
public class Employee
    public Employee(int id, string firstName, string lastName, int salary)
        this.Id = id;
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Salary = salary;

    public int Id;
    public string FirstName;
    public string LastName;
    public int Salary;
Employee[] employees = new Employee[4];
employees[0] = new Employee(1, "David", "Smith", 10000);
employees[1] = new Employee(3, "Mark", "Drinkwater", 30000);
employees[2] = new Employee(4, "Norah", "Miller", 20000);
employees[3] = new Employee(12, "Cecil", "Walker", 120000);

MemoryStream ms = new MemoryStream();
NXmlWriter writer = NXmlWriter.Create(ms);

foreach (Employee employee in employees)
    writer.WriteElementString("ID", employee.Id.ToString());
    writer.WriteElementString("FirstName", employee.FirstName);
    writer.WriteElementString("LastName", employee.LastName);
    writer.WriteElementString("Salary", employee.Salary.ToString());

writer.WriteEndElement(); // Close the "Employees" element

 The result of this piece of code will be the following XML file:

NXmlWriter Example Output
Copy Code
<?xml version="1.0" encoding="utf-8"?>
See Also
Send Feedback